% =========================
% custom-cv.cls  (2025-08-13)
% =========================
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{custom-cv}[2025/08/13 Custom CV class]

% Base class
\LoadClass[11pt,a4paper]{article}

% Packages
\RequirePackage[a4paper,margin=1in]{geometry}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{tikz}
\RequirePackage{titlesec}
\RequirePackage{enumitem}
\RequirePackage{hyperref}
\RequirePackage{draftwatermark}
\RequirePackage{fontspec}
\RequirePackage{pgf} % for watermark autoscale

% ---------------------------
% Fonts (with fallback)
% ---------------------------
\IfFontExistsTF{Helvetica Neue}{
  \setmainfont{Helvetica Neue}
}{
  \IfFontExistsTF{TeX Gyre Heros}{\setmainfont{TeX Gyre Heros}}{\setmainfont{Latin Modern Roman}}
}

% ---------------------------
% Colors
% ---------------------------
\definecolor{cvblue}{HTML}{1A4E8A}

% Debug colors & borders
\definecolor{debugtext}{HTML}{E0F7FA}
\definecolor{debugtextborder}{HTML}{0D47A1}
\definecolor{debugphoto}{HTML}{FFEBEE}
\definecolor{debugphotoborder}{HTML}{B71C1C}

% ---------------------------
% Debug flag & helpers
% ---------------------------
\newif\ifcvdebug
\cvdebugfalse

\newcommand{\debugboxtext}[1]{%
  \ifcvdebug
    \fcolorbox{debugtextborder}{debugtext}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{#1}}%
  \else
    #1%
  \fi
}

\newcommand{\debugboxphoto}[1]{%
  \ifcvdebug
    \fcolorbox{debugphotoborder}{debugphoto}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{#1}}%
  \else
    #1%
  \fi
}

% ---------------------------
% CV storage + setters
% ---------------------------
\newcommand{\cvtitle}{}    % main title (name)
\newcommand{\cvauthor}{}   % contact line
\newcommand{\cvphoto}{}    % photo path

\newcommand{\setcvtitle}[1]{\renewcommand{\cvtitle}{#1}}
\newcommand{\setcvauthor}[1]{\renewcommand{\cvauthor}{#1}}
\newcommand{\setcvphoto}[1]{\renewcommand{\cvphoto}{#1}}

% ---------------------------
% Robust "is empty?" test (trim spaces)
% ---------------------------
\makeatletter
\newcommand{\IfNotEmpty}[3]{%
  \edef\cv@tmp{\zap@space\detokenize{#1} \@empty}%
  \ifx\cv@tmp\@empty
    #3%
  \else
    #2%
  \fi
}
\makeatother

% ---------------------------
% Hyperlinks & lists
% ---------------------------
\hypersetup{
  colorlinks=true,
  urlcolor=cvblue,
  linkcolor=cvblue
}
\setlength{\parskip}{0.4em}
\setlist[itemize]{noitemsep,topsep=0pt}

% ---------------------------
% Section formatting
% ---------------------------
\titleformat{\section}
  {\large\bfseries\color{cvblue}}
  {}{0em}
  {\debugboxtext}
  [\titlerule]

\titleformat{\subsection}
  {\normalsize\bfseries\color{cvblue}}
  {}{0em}
  {}

\setcounter{secnumdepth}{0}

% ---------------------------
% CV content commands
% ---------------------------
\newcommand{\cvitem}[2]{%
  \debugboxtext{%
    \noindent\textbf{#1:} #2
  }\par
}

% \cventry{Title}{Date/Place}{Subtitle/Role}{Details}
\newcommand{\cventry}[4]{%
  \debugboxtext{%
    \noindent\textbf{#2} \hfill {\small #1}\\
    \IfNotEmpty{#3}{\textit{#3}\par}{}%
    #4%
  }\par\vspace{0.8em}
}

% ---------------------------
% maketitle with adaptive layout
% ---------------------------
\renewcommand{\maketitle}{%
  \IfNotEmpty{\cvtitle}{%
    \begin{center}
      \IfNotEmpty{\cvphoto}{%
        % Text + Photo
        \begin{minipage}[c]{0.75\textwidth}
          \debugboxtext{%
            {\LARGE \textbf{\cvtitle}}%
            \IfNotEmpty{\cvauthor}{\\[0.5em]\cvauthor}{}%
          }
        \end{minipage}%
        \hfill
        \begin{minipage}[c]{0.23\textwidth}
          \debugboxphoto{%
            \begin{tikzpicture}
              \clip (0,0) circle (1.75cm);
              \node at (0,0) {\includegraphics[height=3.5cm]{\cvphoto}};
            \end{tikzpicture}
          }
        \end{minipage}
      }{%
        % No photo: full-width text
        \begin{minipage}[c]{\textwidth}
          \debugboxtext{%
            {\LARGE \textbf{\cvtitle}}%
            \IfNotEmpty{\cvauthor}{\\[0.5em]\cvauthor}{}%
          }
        \end{minipage}
      }%
    \end{center}
    \vspace{1em}
  }{}%
}

% ---------------------------
% Watermark (auto-scale)
% ---------------------------
\newlength{\cvWatermarkWidth}
\newcommand{\cvsetwatermark}{%
  \ifcvdebug
    \SetWatermarkText{DEBUG MODE}%
    {%
      \fontsize{5cm}{5cm}\selectfont
      \settowidth{\cvWatermarkWidth}{DEBUG MODE}%
    }%
    \pgfmathsetmacro{\cvScale}{min(\paperwidth,\paperheight)/\cvWatermarkWidth*0.9}%
    \SetWatermarkScale{\cvScale}%
    \SetWatermarkColor[gray]{0.85}%
  \else
    \SetWatermarkText{}%
  \fi
}
\AtBeginDocument{\cvsetwatermark}
