\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{thesis}[2025/05/10 Bachelor Thesis Class]

\LoadClass[a4paper,12pt]{report}

% Load common packages
\RequirePackage{fontspec}
\RequirePackage{titlesec}
\RequirePackage{fancyhdr}
\RequirePackage{geometry}
\RequirePackage{setspace}
\RequirePackage{graphicx}
\RequirePackage{hyperref}
\RequirePackage{etoolbox}
\RequirePackage{tocloft}
\RequirePackage{tocbibind}

% Page layout
\geometry{margin=1in}
\setlength{\parskip}{0.5em}
\setlength{\parindent}{0pt}
\onehalfspacing

% Fonts
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}

% Metadata commands
\input{metadata} % Load metadata from external file

\newcommand{\setthesisinfo}[7]{%
  \renewcommand{\thesistitle}{#1}%
  \renewcommand{\studentname}{#2}%
  \renewcommand{\studentid}{#3}%
  \renewcommand{\program}{#4}%
  \renewcommand{\faculty}{#5}%
  \renewcommand{\university}{#6}%
  \renewcommand{\yearofsubmission}{#7}%
}

% Header and footer
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\nouppercase{\rightmark}}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyfoot[C]{\thepage}

% Chapter formatting
\titleformat{\chapter}[display]
  {\bfseries\Large\centering}
  {BAB~\Roman{chapter}} % << display format
  {1ex}
  {\MakeUppercase}

% Ensure chapter reference in TOC matches
\renewcommand{\cftchappresnum}{BAB~}
\renewcommand{\cftchapaftersnum}{\quad}

% \titlespacing*{\chapter}{0pt}{-10pt}{20pt}

% Redefine \maketitle
\renewcommand{\maketitle}{\input{frontmatter/maketitle}}

% Chapter & Section format
\renewcommand{\cftchapfont}{\bfseries\MakeUppercase}
% \renewcommand{\cftsecfont}{}
% \renewcommand{\cftsubsecfont}{\itshape}
% \renewcommand{\thesection}{\textup{\Roman{chapter}}.\arabic{section}}


% Dot leaders, spacing, indentation
\setlength{\cftbeforechapskip}{0em}
\setlength{\cftchapindent}{0pt}
\setlength{\cftsecindent}{0em}
\setlength{\cftsubsecindent}{3em}
\setlength{\cftchapnumwidth}{4em}
\setlength{\cftsecnumwidth}{2.5em}
\setlength{\cftsubsecnumwidth}{2.5em}
\renewcommand \cftchapdotsep{4.5}
\renewcommand \cftchapdotsep{4.5} % https://tex.stackexchange.com/a/273764
\renewcommand{\cftchapleader}{\normalfont\cftdotfill{\cftsecdotsep}}
\renewcommand{\cftchappagefont}{\normalfont}

% Title styling
\renewcommand{\contentsname}{\hfill\bfseries\normalsize DAFTAR ISI\hfill}
\renewcommand{\cfttoctitlefont}{\bfseries\MakeUppercase}
% \renewcommand{\cftaftertoctitle}{\vskip 2em}

\renewcommand{\tableofcontents}{
  \clearpage
  \pagestyle{plain} % Roman page number, centered bottom
  \pagenumbering{roman}
  \setcounter{page}{viii} % Optional: sync with expected numbering
  \addcontentsline{toc}{chapter}{DAFTAR ISI}
  \oldtableofcontents
  \clearpage
  \pagenumbering{arabic}
  \setcounter{page}{1} % Start chapter 1 at page 1
}

% Frontmatter Macro (Toggle TOC Inclusion)
\newcommand{\frontmattersection}[3]{%
  \begin{titlepage}
  \centering
  \ifstrequal{#1}{toc}{\section*{#2}\addcontentsline{toc}{section}{#2}}{\section*{#2}}%
  \input{frontmatter/#3}
  \end{titlepage}
  \clearpage
}

% Wrapper Command for Each Page
\newcommand{\theendorsementpage}[1]{\frontmattersection{#1}{Pengesahan}{endorsement}}
\newcommand{\originalitystatement}[1]{\frontmattersection{#1}{Pernyataan Keaslian}{originality}}


\endinput