% =============================================================================
%          file:  fistandrose.sty
%   description:  "Fist and Rose" journal layer on top of wpi.sty
%        author:  Victoria A. Stuart
%       created:  2026-Oct-15
%       version:  v01 [2026-Oct-15] DRAFT
%      based on:  wpi.sty (itself based on arxiv.sty)
% -----------------------------------------------------------------------------
% USE  (set the flag BEFORE \input{preamble}; the preamble then loads this file
%       instead of wpi.sty -- see preamble.patch):
%
%   \newcommand{\FRjournal}{}
%   \input{preamble}
%
%   \title{...}
%   \FRauthor[0000-0000-0000-0000]{Author One}{1}     % [ORCID] optional; {keys}
%   \FRauthor{Author Two}{1,2}
%   \FRaffil{1}{Department, Institution, City, Country}
%   \FRaffil{2}{Independent researcher, City, Country}
%   \FRcorresp{author@example.org}
%   \FRarticletype{Research article}
%   \FRvolume{1}  \FRissue{1}  \FRyear{2027}
%   \FRreceived{2026-Oct-14}  \FRaccepted{...}  \FRpublished{...}
%   \FRversion{v1}  \FRupdated{2027-Jan-15}           % living documents
%   \FRdoi{10.0000/example}  \FRlicense{CC BY 4.0}
%   \FRkeywords{wealth \and power \and influence}
%   \FRrunningtitle{Short title for the page header}  % optional
%   \FRrevision{v1}{2027-Jan-15}{First publication}   % repeatable
%
%   \begin{document}\maketitle
%   \begin{abstract}...\end{abstract}  \FRprintkeywords  \FRcitebox
%   ...  \FRprintrevisions   % optional "Version history" table
%
% Dates: write them as yyyy-mmm-dd (e.g. 2027-Jan-15).
% =============================================================================

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fistandrose}[2026/10/15 v01 Fist and Rose journal layer]

\RequirePackage{etoolbox}
\RequirePackage{wpi}
\RequirePackage{orcidlink}

\makeatletter

% ---- storage ----------------------------------------------------------------
\newcommand{\FR@authors}{}      % typeset author line
\newcommand{\FR@names}{}        % plain names (PDF metadata, cite string)
\newcommand{\FR@affils}{}       % typeset affiliations
\newcommand{\FR@revrows}{}      % version-history rows
\newcommand{\FR@kw}{}
\newcommand{\FR@runtitle}{}
\newcommand{\FR@type}{}
\newcommand{\FR@corresp}{}

\newcommand{\FR@define}[2]{%  \FR@define{Name}{default}: defines \FR@Name and \FRName
  \expandafter\newcommand\csname FR@#1\endcsname{#2}%
  \expandafter\newcommand\csname FR#1\endcsname[1]{\expandafter\renewcommand\csname FR@#1\endcsname{##1}}%
}
\FR@define{volume}{}   \FR@define{issue}{}     \FR@define{year}{}
\FR@define{received}{} \FR@define{accepted}{}  \FR@define{published}{}
\FR@define{version}{}  \FR@define{updated}{}
\FR@define{doi}{}      \FR@define{license}{}

\newcommand{\FRarticletype}[1]{\renewcommand{\FR@type}{#1}}
\newcommand{\FRrunningtitle}[1]{\renewcommand{\FR@runtitle}{#1}}
\newcommand{\FR@kwpdf}{}
% keywords: separate with \and; a plain comma-separated copy goes to the PDF properties
\def\FR@kwloop#1\and#2\FR@kwend{%
  \edef\FR@tmp{\zap@space#1 \@empty}%
  \ifdefempty{\FR@kwpdf}{}{\gappto\FR@kwpdf{, }}%
  \expandafter\gappto\expandafter\FR@kwpdf\expandafter{\FR@tmp}%
  \ifblank{#2}{}{\FR@kwloop#2\FR@kwend}}
\newcommand{\FRkeywords}[1]{%
  \renewcommand{\FR@kw}{#1}\renewcommand{\FR@kwpdf}{}%
  \FR@kwloop#1\and\FR@kwend}
\newcommand{\FRcorresp}[1]{\renewcommand{\FR@corresp}{#1}}

% \FRauthor[orcid]{name}{affiliation keys}
\newcommand{\FRauthor}[3][]{%
  \ifdefempty{\FR@authors}{}{\gappto\FR@authors{,\ }}%
  \ifdefempty{\FR@names}{}{\gappto\FR@names{, }}%
  \gappto\FR@names{#2}%
  \gappto\FR@authors{\textbf{#2}\textsuperscript{#3}}%
  \ifblank{#1}{}{\gappto\FR@authors{\,\orcidlink{#1}}}%
}
% \FRaffil{key}{text}
\newcommand{\FRaffil}[2]{\gappto\FR@affils{\textsuperscript{#1}~#2\par}}
% \FRrevision{version}{date}{note}
\newcommand{\FRrevision}[3]{\gappto\FR@revrows{#1 & #2 & #3 \\}}

% ---- header / footer (extends wpi.sty's fancyhdr setup) ----------------------
\newcommand{\FR@volissue}{%
  \ifdefempty{\FR@volume}{}{Vol.~\FR@volume\ifdefempty{\FR@issue}{}{(\FR@issue)}}%
  \ifdefempty{\FR@year}{}{\ \FR@year}}
\renewcommand{\headeright}{Fist and Rose\ifdefempty{\FR@volume}{}{\ \textperiodcentered\ \FR@volissue}}
\renewcommand{\shorttitle}{\ifdefempty{\FR@runtitle}{\@title}{\FR@runtitle}}
\lfoot{\footnotesize\ifdefempty{\FR@version}{}{\FR@version}%
  \ifdefempty{\FR@updated}{}{\ \textperiodcentered\ updated \FR@updated}}
\rfoot{\footnotesize\ifdefempty{\FR@doi}{}{doi:\FR@doi}}

% first page: no header, same footer
\fancypagestyle{FRfirst}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0pt}%
  \cfoot{\thepage \hspace{1pt} of \pageref*{LastPage}}%
  \lfoot{\footnotesize\ifdefempty{\FR@version}{}{\FR@version}%
    \ifdefempty{\FR@updated}{}{\ \textperiodcentered\ updated \FR@updated}}%
  \rfoot{\footnotesize\ifdefempty{\FR@doi}{}{doi:\FR@doi}}%
}

% ---- title block --------------------------------------------------------------
\newcommand{\FR@metaitem}[2]{\ifdefempty{#2}{}{\textbf{#1}~#2\quad}}

\renewcommand{\maketitle}{%
  \par
  \begingroup
    \renewcommand{\thefootnote}{\fnsymbol{footnote}}
    \long\def\@makefntext##1{\parindent 1em\noindent\hbox to 1.8em{\hss $\m@th ^{\@thefnmark}$}##1}
    \thispagestyle{FRfirst}
    \@maketitle
    \@thanks
  \endgroup
  \let\maketitle\relax
  \let\thanks\relax
}

\renewcommand{\@maketitle}{%
  \vbox{%
    \hsize\textwidth \linewidth\hsize
    \vskip \WPItitleTop
    \@toptitlebar
    \centering
    \ifdefempty{\FR@type}{}{{\footnotesize\bfseries\MakeUppercase{\FR@type}\par}\vskip 1.5mm}%
    {\LARGE\@title\par}
    \@bottomtitlebar
    \vskip 2.5mm
    {\FR@authors\par}
    \vskip 1.5mm
    {\footnotesize\FR@affils}%
    \ifdefempty{\FR@corresp}{}{{\footnotesize Corresponding author: \href{mailto:\FR@corresp}{\FR@corresp}\par}}%
    \vskip 1.5mm
    {\footnotesize
     \FR@metaitem{Received}{\FR@received}\FR@metaitem{Accepted}{\FR@accepted}%
     \FR@metaitem{Published}{\FR@published}\FR@metaitem{Version}{\FR@version}%
     \ifdefempty{\FR@doi}{}{\textbf{DOI}~\href{https://doi.org/\FR@doi}{\FR@doi}\quad}%
     \FR@metaitem{License}{\FR@license}\par}
    \vskip 1.5mm
    \hrule height \WPItitleRule
    \vskip \WPItitleBelow
  }}

% ---- keywords, citation box, version history ---------------------------------
\newcommand{\FRprintkeywords}{\ifdefempty{\FR@kw}{}{\expandafter\keywords\expandafter{\FR@kw}}}

\newcommand{\FR@citestring}{%
  \FR@names\ifdefempty{\FR@year}{}{\ (\FR@year)}. \@title.
  \emph{Fist and Rose}\ifdefempty{\FR@volume}{}{\ \FR@volume\ifdefempty{\FR@issue}{}{(\FR@issue)}}%
  \ifdefempty{\FR@version}{}{, \FR@version}%
  \ifdefempty{\FR@updated}{}{\ (updated \FR@updated)}.%
  \ifdefempty{\FR@doi}{}{\ doi:\href{https://doi.org/\FR@doi}{\FR@doi}}}
\newcommand{\FRcitebox}{%
  \par\medskip\noindent
  \fbox{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}{\footnotesize
    \textbf{How to cite:} \FR@citestring}}\par\medskip}

\newcommand{\FRprintrevisions}{%
  \ifdefempty{\FR@revrows}{}{%
    \section*{Version history}%
    \begin{tabular}{@{}lll@{}}\hline
      \textbf{Version} & \textbf{Date} & \textbf{Change} \\ \hline
      \FR@revrows \hline
    \end{tabular}\par}}

% ---- PDF properties (hyperref is loaded later by preamble.tex) -----------------
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
    \begingroup
      \hypersetup{pdftitle={\@title}, pdfauthor={\FR@names},
        pdfkeywords={\FR@kwpdf}, pdfsubject={Fist and Rose \FR@volissue}}%
    \endgroup
  }{}}

\makeatother
\endinput
