% Swiss Business Letter Template for Pandoc
% This template follows Swiss standards (SN) for business correspondence
% Compatible with KOMA-Script scrlttr2 class

\documentclass[
  fontsize=11pt,
  paper=a4,
  parskip=half,           % Half line spacing between paragraphs
  fromalign=right,        % Sender address aligned right
  fromphone,              % Include phone number
  fromemail,              % Include email address
  pagenumber=false,       % Disable page numbers
  version=last            % Use latest KOMA-Script features
]{scrlttr2}

% ==================== ENCODING & FONTS ====================
% UTF-8 input encoding for special characters (ä, ö, ü, ß)
\usepackage[utf8]{inputenc}
% T1 font encoding for proper hyphenation and special characters
\usepackage[T1]{fontenc}

% ==================== HYPERLINKS ====================
% Enable clickable email links in PDF
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  urlcolor=black,
  pdfborder={0 0 0}
}

% ==================== PANDOC COMPATIBILITY ====================
% Define tightlist command for Pandoc-generated lists
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% ==================== LANGUAGE SUPPORT ====================
% Try to load German language support if available
% This provides proper German labels (Telefon, Anlagen) and hyphenation
%
% If german.ldf is found: Labels will be in German
% If not found: Labels will be in English (Phone, encl)
%
% To install German support:
% Ubuntu/Debian: sudo apt install texlive-lang-german
% Arch: sudo pacman -S texlive-langgerman
% macOS/Windows: Included in full TeX Live installation
\IfFileExists{ngerman.ldf}{%
  \usepackage[ngerman]{babel}
}{%
  \IfFileExists{german.ldf}{%
    \usepackage[german]{babel}
  }{%
    % No German language files found
    % Labels will be in English (Phone, encl) without language package
  }
}

% ==================== SWISS LETTER CONFIGURATION ====================
% Load Swiss Norm (SN) for proper address window positioning
% This ensures the recipient address fits in standard Swiss envelope windows
\LoadLetterOption{SN}

% ==================== SENDER INFORMATION ====================
% Customize these fields by editing the markdown frontmatter
\setkomavar{fromname}{$from.name$}
\setkomavar{fromaddress}{$from.street$\\$from.city$}

$if(from.phone)$
\setkomavar{fromphone}{$from.phone$}
$endif$

$if(from.email)$
% Email is rendered as a clickable hyperlink
\setkomavar{fromemail}{\href{mailto:$from.email$}{$from.email$}}
$endif$

% ==================== LETTER METADATA ====================
$if(subject)$
\setkomavar{subject}{$subject$}
$endif$

$if(date)$
\setkomavar{date}{$date$}
$endif$

$if(signature)$
\setkomavar{signature}{$signature$}
$endif$

% ==================== ENCLOSURES ====================
$if(enclosures)$
% Separator between "Enclosures" label and list (e.g., "Anlagen:")
\setkomavar{enclseparator}{:}
$endif$

% ==================== CUSTOMIZATION OPTIONS ====================
% Remove backaddress line in envelope window (uncomment to enable)
\setkomavar{backaddress}{}

% You can customize additional elements here:
% - \setkomavar{location}{} - Text in the upper right corner
% - \setkomavar{place}{} - Place of writing (if different from address)
% - Font sizes, margins, spacing can be adjusted via KOMA-Script options

% ==================== DOCUMENT BODY ====================
\begin{document}

\begin{letter}{%
% Recipient address - each line from the 'to' frontmatter array
$for(to)$
$to$\\
$endfor$
}

% ==================== OPENING ====================
$if(opening)$
\opening{$opening$}
$else$
% Default opening if not specified
\opening{Sehr geehrte Damen und Herren}
$endif$

% ==================== LETTER BODY ====================
% The main content from your markdown file is inserted here
$body$

% ==================== CLOSING ====================
$if(closing)$
\closing{$closing$}
$else$
% Default closing if not specified
\closing{Freundliche Grüsse}
$endif$

% ==================== ENCLOSURES LIST ====================
$if(enclosures)$
% List of attached documents (e.g., CV, certificates)
\encl{$for(enclosures)$$enclosures$$sep$\\$endfor$}
$endif$

\end{letter}

\end{document}
