// ═══════════════════════════════════════════════════════════════════════════
// Template: tiefletter-gold — Editorial luxury (restrained)
// ═══════════════════════════════════════════════════════════════════════════

#import "../shared/invoice.typ": sample-data, compute-totals, resolve-totals, star-mark, money
#import "../shared/components.typ": *

#let d = sample-data
#let totals = resolve-totals(d)

#let theme = (
  ink: rgb("#1A1512"),
  paper: rgb("#FBF9F4"),
  accent: rgb("#8B6F3A"),
  accent-soft: rgb("#F1EBDD"),
  mute: rgb("#74695E"),
  hair: rgb("#D9CFB8"),
  dim: rgb("#C7BFAE"),
  display-font: ("Playfair Display", "Didot", "Georgia"),
  body-font: ("Adobe Garamond Pro", "Iowan Old Style", "Baskerville", "Georgia"),
  mono-font: ("Menlo", "DejaVu Sans Mono"),
  label-style: "smallcaps",
  tax-zero: "dash",
  totals-variant: "framed-gold",
  hide-zero-tax: true,
  qr-style: "rounded",
  margin: (top: 24mm, bottom: 22mm, left: 24mm, right: 24mm),
)

#show: body => page-shell(theme, d.issuer, d.invoice, body)

#set text(
  font: theme.body-font,
  size: 9.5pt,
  fill: theme.ink,
  lang: "en",
  number-type: "lining",
  number-width: "tabular",
)
#set par(leading: 6.4pt, spacing: 6.8pt, justify: false)

// ─── HERO ────────────────────────────────────────────────────────────────
#grid(
  columns: (1fr, auto),
  align: (left + horizon, right + horizon),
  column-gutter: 10mm,
  [
    #if "logo" in d.issuer and d.issuer.logo != none [
      #image(d.issuer.logo, height: 12mm)
      #v(2mm)
    ]
    #grid(
      columns: (auto, auto),
      column-gutter: 8pt,
      align: (horizon, horizon),
      star-mark(size: 12pt, color: theme.accent),
      fit-size(
        (14pt, 12pt, 10.5pt),
        90mm,
        s => text(font: theme.body-font, size: s, weight: 600, fill: theme.ink)[#d.issuer.name],
      ),
    )
    #if "legal-name" in d.issuer and d.issuer.legal-name != none and d.issuer.legal-name != d.issuer.name [
      #v(2pt)
      #text(size: 8.5pt, style: "italic", fill: theme.mute)[#d.issuer.legal-name]
    ]
  ],
  [
    #align(right, text(font: theme.display-font, size: 26pt, weight: 400, style: "italic", fill: theme.ink)[#d.invoice.title])
    #v(-1pt)
    #align(right, fit-size(
      (10pt, 9.5pt, 9pt),
      80mm,
      s => text(font: theme.body-font, size: s, tracking: 0.8pt, fill: theme.accent)[N\u{00BA} #d.invoice.number],
    ))
    #if d.invoice.kind == "credit-note" and d.invoice.credits-number != none [
      #align(right, text(size: 8.5pt, style: "italic", fill: theme.mute)[re: Invoice N\u{00BA} #d.invoice.credits-number])
    ]
  ],
)

#v(mm-sp.s)
#line(length: 100%, stroke: 0.4pt + theme.accent)
#v(mm-sp.s)

// ─── PARTIES ─────────────────────────────────────────────────────────────
#grid(
  columns: (1fr, 1fr),
  column-gutter: 14mm,
  party-block(d.client, theme, label-text: "Bill to"),
  party-block(d.issuer, theme, label-text: "Bill from", show-name: false),
)

#v(mm-sp.s)
#line(length: 100%, stroke: 0.3pt + theme.hair)
#v(mm-sp.xs)

// ─── META ────────────────────────────────────────────────────────────────
#let meta-cell(label, value, emphasize: false) = [
  #lbl(theme, label)
  #v(sp.xs)
  #if emphasize {
    text(size: 9.5pt, weight: 600, fill: theme.accent)[#value]
  } else {
    text(size: 9.5pt)[#value]
  }
]

#grid(
  columns: (auto, auto, 1fr, auto),
  column-gutter: 10mm,
  align: (left + top, left + top, left + top, left + top),
  meta-cell("Invoice date", d.invoice.issue-date),
  meta-cell("Due date",     d.invoice.due-date, emphasize: true),
  meta-cell("Terms",        d.invoice.terms),
  meta-cell("Currency",     d.invoice.currency),
)

#v(mm-sp.m)

// ─── ITEMS + TOTALS ──────────────────────────────────────────────────────
#line-items-table(d.items, theme, currency-symbol: d.invoice.symbol, tax-label: d.invoice.tax-label)

// Line-level discount summary — editorial italic, gold accent.
#let discounted-items = d.items.filter(it => "discount" in it and it.discount != none)
#if discounted-items.len() > 0 [
  #v(sp.xs)
  #align(right)[
    #box(width: 90mm)[
      #for it in discounted-items [
        #grid(
          columns: (1fr, auto),
          column-gutter: sp.l,
          align: (left, right),
          text(size: 8pt, style: "italic", fill: theme.mute)[
            #it.description ·
            #if it.discount-label != none and it.discount-label.starts-with("rate:") {
              "less " + it.discount-label.slice(5) + "%"
            } else { "discount" }
          ],
          text(size: 8pt, style: "italic", fill: theme.mute)[−#money(it.discount, symbol: d.invoice.symbol)],
        )
      ]
    ]
  ]
]

#v(mm-sp.s)
#tax-totals(totals, theme, currency-symbol: d.invoice.symbol, width: 90mm, tax-label: d.invoice.tax-label, total-label: if d.invoice.kind == "credit-note" { "Total credit" } else { none })

// Invoice-level discount row.
#if "discount" in totals and totals.discount != none [
  #v(sp.s)
  #align(right)[
    #box(width: 90mm)[
      #grid(
        columns: (1fr, auto),
        column-gutter: sp.xl,
        align: (left, right),
        text(size: 9.5pt, style: "italic", fill: theme.mute)[#if totals.discount-label != none { totals.discount-label } else { "Discount" }],
        text(size: 9.5pt, fill: theme.accent)[−#money(totals.discount, symbol: d.invoice.symbol)],
      )
    ]
  ]
]

// Reverse-charge callout — hairline gold frame matching editorial feel.
#if d.invoice.reverse-charge [
  #v(mm-sp.m)
  #block(width: 100%, inset: 8pt, stroke: 0.3pt + theme.hair, [
    #text(weight: "medium", size: 9pt, fill: theme.ink)[Reverse charge]\
    #text(size: 8pt, style: "italic", fill: theme.mute)[VAT to be accounted for by the recipient under the reverse-charge mechanism.]
  ])
]

// ─── PAYMENT + NOTES ─────────────────────────────────────────────────────
#v(mm-sp.m)
#let payment-label = if d.invoice.kind == "credit-note" { "Payment details" } else { "Remittance" }
#block(breakable: false)[
  #if "qr" in d and d.qr != none {
    grid(
      columns: (1fr, 1fr, auto),
      column-gutter: 12mm,
      payment-block(d.issuer.bank, theme, label-text: payment-label),
      notes-block(d.notes, theme),
      [
        #qr-render(d.qr.modules, size: 24mm, fg: theme.accent, bg: theme.accent-soft, style: theme.qr-style)
        #v(2pt)
        #align(center, text(font: theme.body-font, size: 7pt, style: "italic", fill: theme.mute, tracking: 0.5pt)[#d.qr.label])
      ],
    )
  } else {
    grid(
      columns: (1fr, 1fr),
      column-gutter: 12mm,
      payment-block(d.issuer.bank, theme, label-text: payment-label),
      notes-block(d.notes, theme),
    )
  }
]
