#import "utils.typ": format-eur

#let data_file = sys.inputs.at("data_path", default: "none")

#let data = if data_file != "none" { json(data_file) } else {
  (
    "ordineId": "3df6bd0d-2d2d-4687-b4b9-87f2aecc297c",
    "date": "1 gennaio 2026",
    "datetime": "14:55",
    "nominativo": "Di Grigoli Angelo Damiano",
    "codicefiscale": "DGRNLD97C13B602Z",
    "indirizzo": "Via Aldo Fantina 15, PIEVE DEL GRAPPA (TV) 31017",
    "nazione": "Italia",
    "items": (
      (
        "title": "Acconto Cerco Transitorio",
        "desc": none,
        "discount": none,
        "amount": 3500,
        "strike": false,
      ),
      (
        "title": "Saldo Cerco Affitto Transitorio (6 mesi)",
        "desc": "Pagamento differito, non è dovuto in questo momento",
        "discount": none,
        "amount": 37500,
        "strike": true,
      ),
    ),
    "total": 3500,
    "vat": 770,
    "method": "Carta di credito",
  )
}


#set page(
  paper: "a4",
  margin: (x: 1cm, y: 1cm),
  footer: [
    #set align(center)
    #set text(size: 9pt, fill: luma(100))
    Arcenia Srl.| P.IVA 00924740244 | Tel. +39 0424529869 | Email: arca\@infoalloggi.it
  ],
)

#set text(font: "Inter 18pt", size: 10pt)

// --- Header ---
#grid(
  columns: (1fr, auto),
  [
    #text(size: 14pt, weight: "bold", fill: rgb("1a202c"))[Arcenia S.r.l.] \
    Via Beata Giovanna 1 \
    Bassano del Grappa (VI) 36061 \
    Italia
  ],
  [
    #image("/public/favicon/favicon.png", width: 80pt)
  ],
)

#v(2.5em)

// --- Title ---
#align(center)[
  #text(size: 16pt, weight: "bold", fill: rgb("1a202c"))[Ricevuta di cortesia]
]

#v(2em)

#grid(
  columns: (1fr, auto),
  [
    #text(fill: luma(120), size: 9pt)[ID Ordine] \
    #data.ordineId
  ],
  align(right)[
    #text(fill: luma(120), size: 9pt)[Data] \
    #text(size: 12pt)[#data.date] \
    #data.datetime
  ],
)

#v(0.5em)
#line(length: 100%, stroke: 0.5pt + luma(200))
#v(1em)


#text(weight: "bold")[Intestatario:] \
#v(0.2em)
#data.nominativo \
#data.codicefiscale \
#data.indirizzo \
#data.nazione
#v(2.5em)


#table(
  columns: (1fr, auto, auto),
  stroke: none,
  align: (left, right, right),
  inset: (x: 5pt, y: 10pt),

  // Header row
  [*Oggetto*], [*Sconto applicato*], [*Importo*],
  table.hline(stroke: 0.5pt + luma(200)),

  ..data
    .items
    .map(item => {
      (
        item.title + if item.desc != none { "\n" + text(size: 9pt, fill: luma(100))[#item.desc] } else { "" },
        if item.at("discount", default: none) != none {
          if item.discount > 0 {
            [#item.discount %]
          }
        } else [],
        if item.at("strike", default: false) {
          strike(stroke: 1pt + luma(100))[#text(fill: luma(100))[#format-eur(item.amount)]]
        } else {
          [#format-eur(item.amount)]
        },
        table.hline(stroke: 0.5pt + luma(200)),
      )
    })
    .flatten(),
)

// --- Totals ---
#v(0.5em)
#align(right)[
  #grid(
    columns: (1fr, auto),
    align: (right, right),
    gutter: 1.5em,
    [*Totale:*], [*#format-eur(data.total)*],
    [#text(fill: luma(100))[di cui IVA (22%):]], [#text(fill: luma(100))[#format-eur(data.vat)]],
  )
]

#v(2em)
#line(length: 100%, stroke: 0.5pt + luma(200))
#v(0.5em)


#grid(
  columns: (1fr, auto),
  [
    #text(weight: "bold")[Metodo di pagamento] \
    #v(0.2em)
    #data.method
  ],
  align(right)[
    #text(weight: "bold")[Status pagamento] \
    #v(0.2em)
    // Custom hex color for the success text
    #text(fill: rgb("00A650"), weight: "bold")[Pagato]
  ],
)

#v(0.5em)
#line(length: 100%, stroke: 0.5pt + luma(200))
#v(1em)


#text(weight: "bold")[Note] \
#v(0.2em)
#text(size: 9pt, fill: rgb("4a5568"))[
  La presente non costituisce ricevuta o fattura fiscale ai sensi dell'Art.21, DPR 633/72.\
  Seguirà fattura elettronica direttamente al tuo indirizzo di posta.
]
