export type ErrorVariant = "not-found" | "error";
export type ErrorLang = "de" | "en";

export type ReceiptLine = { label: string; amount: string };

export type ReceiptSet = {
  title: string;
  lines: ReceiptLine[];
  subtotal: string;
  total: string;
  status: string;
  footer: string;
  note: string;
};

type Copy = {
  brandMark: string;
  brandSub: string;
  codeLabel: string;
  headline: string;
  body: string;
  primary: string;
  secondary: string;
  printerPlate: string;
  ledIdle: string;
  ledScan: string;
  ledVoid: string;
  ledJam: string;
  ledOk: string;
  customerCopy: string;
  staffCopy: string;
  txn: string;
  txnValue: string;
  date: string;
  ref: string;
  terminal: string;
  scanHint: string;
  scanEmpty: string;
  scanJam: string;
  voidToast: string;
  reprintToast: string;
  persistToast: string;
  digitToast: string;
  staffToast: string;
  excelToast: string;
  konamiToast: string;
  qrToast: string;
  qrPersistToast: string;
  extractLabel: string;
  extractValue: string;
  mathLabel: string;
  mathValue: string;
  fileLabel: string;
  fileSkip: string;
  excelSheet: string;
  barcodeLabel: string;
  sourceComment: string;
  sets: ReceiptSet[];
};

const de404: Copy = {
  brandMark: "SCAN",
  brandSub: "RECEIPTS",
  codeLabel: "ERR 404  NICHT IM JOURNAL",
  headline: "Dieser Gang ist leer.",
  body: "Die Adresse steht nicht im Journal. Kein Beleg, keine Zeile, nichts zu exportieren.",
  primary: "Zur Startseite",
  secondary: "Zum Dashboard",
  printerPlate: "SCAN TO FILE",
  ledIdle: "NO MATCH",
  ledScan: "0 ROWS",
  ledVoid: "VOID",
  ledJam: "JAM",
  ledOk: "OK",
  customerCopy: "KUNDENBELEG",
  staffCopy: "INTERN  NICHT AUSHÄNDIGEN",
  txn: "Vorgang",
  txnValue: "Seitensuche",
  date: "Datum",
  ref: "Ref",
  terminal: "Filiale 04   Kasse 04",
  scanHint: "Beleg scannen",
  scanEmpty: "Scan fertig. 0 Zeilen. Nichts zu verbuchen.",
  scanJam: "Scan hängt. Die Summen gehen nicht auf.",
  voidToast: "Beleg storniert. Immer noch keine Seite.",
  reprintToast: "Nachdruck läuft.",
  persistToast: "Du bist hartnäckig. Der Gang bleibt leer.",
  digitToast: "Du hast den Fehler selbst gescannt. Sauber.",
  staffToast: "Mitarbeiterkopie. Still halten.",
  excelToast: "In die Tabelle gezogen. Die Zeile ist leer.",
  konamiToast: "Journal läuft über. Du gewinnst nichts. Sieht gut aus.",
  qrToast: "Im QR steckt keine fehlende Seite. Geprüft.",
  qrPersistToast: "Immer noch 404. Der QR ist Dekor. Fast.",
  extractLabel: "EXTRACT",
  extractValue: "0 ZEILEN",
  mathLabel: "MATH",
  mathValue: "NICHTS ZU PRÜFEN",
  fileLabel: "FILE",
  fileSkip: "ÜBERSPRUNGEN",
  excelSheet: "Sheet1",
  barcodeLabel: "REPRINT",
  sourceComment:
    "If you are reading this: the page is gone, but the ledger is watching. Type VOID. Click the barcode. Scan the paper.",
  sets: [
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Gewünschte Route", amount: "1,00" },
        { label: "Neugier", amount: "0,19" },
        { label: "Verwirrung 19% MwSt", amount: "0,76" },
        { label: "Seite die existieren sollte", amount: "0,00" },
      ],
      subtotal: "1,95",
      total: "4,04",
      status: "NICHT GEFUNDEN",
      footer: "Vielen Dank fürs Suchen",
      note: "Dieses Regal ist leer",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Tote URL", amount: "2,00" },
        { label: "Hoffnung", amount: "0,04" },
        { label: "Korrekturbeleg", amount: "2,00" },
      ],
      subtotal: "4,04",
      total: "4,04",
      status: "NACHDRUCK",
      footer: "Zweiter Versuch, gleiches Regal",
      note: "Wir haben unter dem Bon geschaut",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Tippfehler im Pfad", amount: "3,80" },
        { label: "Lesezeichen von 2024", amount: "0,24" },
      ],
      subtotal: "4,04",
      total: "4,04",
      status: "ARCHIV LEER",
      footer: "Kein Treffer in 0 Ordnern",
      note: "Excel bleibt unberührt",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Seite die es nie gab", amount: "4,04" },
        { label: "Rabatt für Hartnäckigkeit", amount: "0,00" },
      ],
      subtotal: "4,04",
      total: "4,04",
      status: "ENDGÜLTIG LEER",
      footer: "Letzter Nachdruck für heute",
      note: "Typ VOID wenn du musst",
    },
  ],
};

const en404: Copy = {
  brandMark: "SCAN",
  brandSub: "RECEIPTS",
  codeLabel: "ERR 404  NOT IN LEDGER",
  headline: "This aisle is empty.",
  body: "That address is not in the ledger. No receipt, no row, nothing to export.",
  primary: "Back home",
  secondary: "Dashboard",
  printerPlate: "SCAN TO FILE",
  ledIdle: "NO MATCH",
  ledScan: "0 ROWS",
  ledVoid: "VOID",
  ledJam: "JAM",
  ledOk: "OK",
  customerCopy: "CUSTOMER COPY",
  staffCopy: "STAFF COPY  DO NOT HAND OUT",
  txn: "Txn",
  txnValue: "Page lookup",
  date: "Date",
  ref: "Ref",
  terminal: "Store 04   Till 04",
  scanHint: "Scan receipt",
  scanEmpty: "Scan done. 0 rows. Nothing to file.",
  scanJam: "Scan jammed. The totals do not reconcile.",
  voidToast: "Receipt voided. Still no page.",
  reprintToast: "Reprint feeding.",
  persistToast: "You are persistent. The aisle stays empty.",
  digitToast: "You just scanned the error itself. Clean.",
  staffToast: "Staff copy. Keep it quiet.",
  excelToast: "Dropped into the sheet. The row is empty.",
  konamiToast: "Ledger overflow. You win nothing. Looks good though.",
  qrToast: "The missing page is not in the QR. We checked.",
  qrPersistToast: "Still 404. The QR is decoration. Mostly.",
  extractLabel: "EXTRACT",
  extractValue: "0 ROWS",
  mathLabel: "MATH",
  mathValue: "NOTHING TO VERIFY",
  fileLabel: "FILE",
  fileSkip: "SKIPPED",
  excelSheet: "Sheet1",
  barcodeLabel: "REPRINT",
  sourceComment:
    "If you are reading this: the page is gone, but the ledger is watching. Type VOID. Click the barcode. Scan the paper.",
  sets: [
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Requested route", amount: "1.00" },
        { label: "Curiosity", amount: "0.19" },
        { label: "Confusion tax 19%", amount: "0.76" },
        { label: "Page that should exist", amount: "0.00" },
      ],
      subtotal: "1.95",
      total: "4.04",
      status: "NOT FOUND",
      footer: "Thank you for looking",
      note: "This shelf is empty",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Dead URL", amount: "2.00" },
        { label: "Hope", amount: "0.04" },
        { label: "Correction slip", amount: "2.00" },
      ],
      subtotal: "4.04",
      total: "4.04",
      status: "REPRINT",
      footer: "Second try, same shelf",
      note: "We looked under the paper",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Typo in the path", amount: "3.80" },
        { label: "Bookmark from 2024", amount: "0.24" },
      ],
      subtotal: "4.04",
      total: "4.04",
      status: "ARCHIVE EMPTY",
      footer: "No hit in 0 folders",
      note: "Excel left untouched",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "A page that never existed", amount: "4.04" },
        { label: "Stubbornness discount", amount: "0.00" },
      ],
      subtotal: "4.04",
      total: "4.04",
      status: "FINALLY EMPTY",
      footer: "Last reprint for today",
      note: "Type VOID if you must",
    },
  ],
};

const de500: Copy = {
  ...de404,
  codeLabel: "ERR 500  SUMMENFEHLER",
  headline: "Die Summen gehen nicht auf.",
  body: "Der Beleg ist im Lauf stecken geblieben. Die Prüfung hat die Differenz gefunden, bevor irgendetwas gespeichert wurde.",
  primary: "Erneut versuchen",
  secondary: "Zur Startseite",
  ledIdle: "JAM",
  extractValue: "ABBRUCH",
  mathLabel: "MATH",
  mathValue: "DIFFERENZ",
  fileSkip: "ANGEHALTEN",
  scanJam: "Papierstau. Differenz steht. Nichts verbucht.",
  sets: [
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Position A", amount: "12,00" },
        { label: "Position B", amount: "7,40" },
        { label: "MwSt 19%", amount: "3,69" },
      ],
      subtotal: "23,09",
      total: "50,00",
      status: "SUMMENFEHLER",
      footer: "Brutto ungleich Netto plus Steuer",
      note: "Nichts wurde gespeichert",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Gelesene Summe", amount: "18,20" },
        { label: "Nachgerechnete Summe", amount: "17,45" },
        { label: "Differenz", amount: "0,75" },
      ],
      subtotal: "17,45",
      total: "18,20",
      status: "NICHT ABGESTIMMT",
      footer: "Die Prüfung hat gegriffen",
      note: "Genau deshalb rechnen wir nach",
    },
  ],
};

const en500: Copy = {
  ...en404,
  codeLabel: "ERR 500  SUM MISMATCH",
  headline: "The totals do not reconcile.",
  body: "The slip jammed mid-run. Verification caught the gap before anything was saved.",
  primary: "Try again",
  secondary: "Back home",
  ledIdle: "JAM",
  extractValue: "ABORTED",
  mathLabel: "MATH",
  mathValue: "MISMATCH",
  fileSkip: "HELD",
  scanJam: "Paper jam. Difference stands. Nothing filed.",
  sets: [
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Line A", amount: "12.00" },
        { label: "Line B", amount: "7.40" },
        { label: "Tax 19%", amount: "3.69" },
      ],
      subtotal: "23.09",
      total: "50.00",
      status: "SUM MISMATCH",
      footer: "Gross is not net plus tax",
      note: "Nothing was saved",
    },
    {
      title: "SCANRECEIPTS",
      lines: [
        { label: "Read total", amount: "18.20" },
        { label: "Recomputed total", amount: "17.45" },
        { label: "Difference", amount: "0.75" },
      ],
      subtotal: "17.45",
      total: "18.20",
      status: "UNBALANCED",
      footer: "The check did its job",
      note: "This is why we recompute",
    },
  ],
};

export function getErrorCopy(variant: ErrorVariant, lang: ErrorLang): Copy {
  if (variant === "error") return lang === "de" ? de500 : en500;
  return lang === "de" ? de404 : en404;
}

export function formatReceiptClock(lang: ErrorLang, date = new Date()): string {
  const dd = String(date.getDate()).padStart(2, "0");
  const monthsDe = [
    "01",
    "02",
    "03",
    "04",
    "05",
    "06",
    "07",
    "08",
    "09",
    "10",
    "11",
    "12",
  ];
  const monthsEn = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
  ];
  const hh = String(date.getHours()).padStart(2, "0");
  const mm = String(date.getMinutes()).padStart(2, "0");
  if (lang === "de") {
    return `${dd}.${monthsDe[date.getMonth()]}.${date.getFullYear()}  ${hh}:${mm}`;
  }
  return `${dd} ${monthsEn[date.getMonth()]} ${date.getFullYear()}  ${hh}:${mm}`;
}

export function shortRef(pathname: string): string {
  if (!pathname || pathname === "/") return "/";
  return pathname.length > 28 ? `${pathname.slice(0, 13)}…${pathname.slice(-12)}` : pathname;
}
