import type {
  ActivityEvent,
  BankTx,
  Client,
  ClientQuery,
  Invoice,
  Settings,
} from "./ledger-types";

export const CLIENTS: Client[] = [
  {
    id: "c1",
    name: "Bakkerij De Mol",
    kvk: "62841937",
    snelstart: true,
    lastUpload: "2026-08-11",
  },
  { id: "c2", name: "Studio Noord", kvk: "71048823", snelstart: true, lastUpload: "2026-08-10" },
  { id: "c3", name: "Installatie BV", kvk: "34592017", snelstart: true, lastUpload: "2026-08-11" },
  { id: "c4", name: "Café Haven", kvk: "58210466", snelstart: false, lastUpload: "2026-08-09" },
];

const line = (id: string, description: string, qty: number, unitPrice: number, vatRate = 21) => ({
  id,
  description,
  qty,
  unitPrice,
  vatRate,
});

function inv(p: Partial<Invoice> & Pick<Invoice, "id" | "clientId" | "supplier" | "status">): Invoice {
  return {
    file: `${p.supplier.toLowerCase().replace(/[^a-z]+/g, "-")}-factuur.pdf`,
    number: "F-2026-0001",
    invoiceDate: "2026-08-04",
    dueDate: "2026-09-03",
    vatAmount: 0,
    total: 0,
    currency: "EUR",
    paymentRef: "0000000000000000",
    uploadedAt: "2026-08-05",
    confidence: 90,
    lines: [],
    suggestion: {
      ledger: "Office expenses",
      vatCode: "NL 21%",
      category: "Operating expenses",
      relation: p.supplier,
      reasoning: `Similar ${p.supplier} invoices have previously been booked to Office expenses with VAT code NL21.`,
    },
    ...p,
  } as Invoice;
}

export const INVOICES: Invoice[] = [
  inv({
    id: "INV-2041",
    clientId: "c1",
    supplier: "Meelfabriek Van Dijk",
    number: "MVD-2026-1187",
    invoiceDate: "2026-08-03",
    dueDate: "2026-08-31",
    total: 1245.8,
    vatAmount: 216.19,
    paymentRef: "0091 2244 8817 3390",
    confidence: 94,
    status: "Auto-ready",
    lines: [line("l1", "Tarwebloem T65, 25 kg zakken", 20, 42.5, 9), line("l2", "Transportkosten", 1, 45.0)],
    suggestion: {
      ledger: "Raw materials",
      vatCode: "NL 9%",
      category: "Cost of goods",
      relation: "Meelfabriek Van Dijk",
      reasoning:
        "Similar supplier invoices have previously been booked to Raw materials with VAT code NL9 for this client.",
    },
  }),
  inv({
    id: "INV-2042",
    clientId: "c1",
    supplier: "Koeltechniek Bergman",
    number: "KB-88213",
    invoiceDate: "2026-08-05",
    dueDate: "2026-09-04",
    total: 892.15,
    vatAmount: 154.83,
    confidence: 71,
    status: "Needs review",
    lines: [line("l1", "Onderhoud koelinstallatie", 1, 620.0), line("l2", "Voorrijkosten", 2, 58.66)],
    suggestion: {
      ledger: "Maintenance & repairs",
      vatCode: "NL 21%",
      category: "Operating expenses",
      relation: "Koeltechniek Bergman",
      reasoning:
        "Description mentions maintenance work. Historical bookings for this supplier are split between Maintenance and Equipment.",
    },
  }),
  inv({
    id: "INV-2043",
    clientId: "c2",
    supplier: "Drukkerij Veenstra",
    number: "DV-2026-0442",
    invoiceDate: "2026-08-02",
    dueDate: "2026-08-30",
    total: 468.0,
    vatAmount: 81.22,
    confidence: 88,
    status: "Approved",
    lines: [line("l1", "Drukwerk portfoliomappen", 150, 2.6)],
  }),
  inv({
    id: "INV-2044",
    clientId: "c2",
    supplier: "Adobe Systems Europe",
    number: "ADB-77219340",
    invoiceDate: "2026-08-01",
    dueDate: "2026-08-15",
    total: 719.4,
    vatAmount: 124.86,
    confidence: 97,
    status: "Synced",
    lines: [line("l1", "Creative Cloud teams, 4 seats", 4, 149.85)],
    suggestion: {
      ledger: "Software & subscriptions",
      vatCode: "NL 21%",
      category: "Operating expenses",
      relation: "Adobe Systems Europe",
      reasoning: "Recurring monthly subscription booked to Software & subscriptions for 11 prior periods.",
    },
  }),
  inv({
    id: "INV-2045",
    clientId: "c3",
    supplier: "Technische Unie",
    number: "TU-5540921",
    invoiceDate: "2026-08-06",
    dueDate: "2026-09-05",
    total: 3418.75,
    vatAmount: 593.28,
    confidence: 91,
    status: "Auto-ready",
    lines: [
      line("l1", "Koperen buis 15mm, 5m", 40, 21.4),
      line("l2", "Fittingen assortiment", 12, 89.5),
      line("l3", "CV-ketel montageset", 3, 412.0),
    ],
    suggestion: {
      ledger: "Materials & parts",
      vatCode: "NL 21%",
      category: "Cost of goods",
      relation: "Technische Unie",
      reasoning: "Wholesale parts supplier; last 24 invoices booked to Materials & parts with NL21.",
    },
  }),
  inv({
    id: "INV-2046",
    clientId: "c3",
    supplier: "Shell Nederland",
    number: "SHL-1129084",
    invoiceDate: "2026-08-07",
    dueDate: "2026-08-21",
    total: 284.6,
    vatAmount: 49.39,
    confidence: 62,
    status: "Needs review",
    lines: [line("l1", "Brandstof tankpas 07-2026", 1, 235.21)],
    suggestion: {
      ledger: "Vehicle costs",
      vatCode: "NL 21%",
      category: "Operating expenses",
      relation: "Shell Nederland",
      reasoning:
        "Fuel card statement. Private use share is unknown, so confidence is reduced pending client confirmation.",
    },
  }),
  inv({
    id: "INV-2047",
    clientId: "c4",
    supplier: "Grolsch Horeca",
    number: "GR-2026-3318",
    invoiceDate: "2026-08-04",
    dueDate: "2026-08-25",
    total: 1876.4,
    vatAmount: 325.6,
    confidence: 93,
    status: "Approved",
    lines: [line("l1", "Fusten pils 50L", 14, 108.0), line("l2", "Frisdrank assortiment", 1, 238.0, 9)],
    suggestion: {
      ledger: "Beverage purchases",
      vatCode: "NL 21%",
      category: "Cost of goods",
      relation: "Grolsch Horeca",
      reasoning: "Recurring beverage wholesaler booked to Beverage purchases in all prior periods.",
    },
  }),
  inv({
    id: "INV-2048",
    clientId: "c4",
    supplier: "Eneco Zakelijk",
    number: "EN-90124477",
    invoiceDate: "2026-07-31",
    dueDate: "2026-08-20",
    total: 642.18,
    vatAmount: 111.45,
    confidence: 89,
    status: "Sync failed",
    syncError: "SnelStart is not connected for this client.",
    lines: [line("l1", "Elektra & gas juli 2026", 1, 530.73)],
    suggestion: {
      ledger: "Energy costs",
      vatCode: "NL 21%",
      category: "Operating expenses",
      relation: "Eneco Zakelijk",
      reasoning: "Utility invoices for this client are consistently booked to Energy costs.",
    },
  }),
  inv({
    id: "INV-2049",
    clientId: "c1",
    supplier: "Meelfabriek Van Dijk",
    number: "MVD-2026-1187",
    invoiceDate: "2026-08-03",
    dueDate: "2026-08-31",
    total: 1245.8,
    vatAmount: 216.19,
    paymentRef: "0091 2244 8817 3390",
    confidence: 78,
    status: "Needs review",
    duplicateOf: "INV-2041",
    lines: [line("l1", "Tarwebloem T65, 25 kg zakken", 20, 42.5, 9)],
    suggestion: {
      ledger: "Raw materials",
      vatCode: "NL 9%",
      category: "Cost of goods",
      relation: "Meelfabriek Van Dijk",
      reasoning: "Matches an existing document on supplier, amount, date and payment reference.",
    },
  }),
  inv({
    id: "INV-2050",
    clientId: "c2",
    supplier: "Coolblue Zakelijk",
    number: "CB-4471029",
    invoiceDate: "2026-08-08",
    dueDate: "2026-08-22",
    total: 2189.0,
    vatAmount: 379.85,
    confidence: 84,
    status: "AI processed",
    lines: [line("l1", "Monitor 27\" 4K", 2, 549.0), line("l2", "Laptop dock", 2, 179.0)],
    suggestion: {
      ledger: "Equipment",
      vatCode: "NL 21%",
      category: "Fixed assets",
      relation: "Coolblue Zakelijk",
      reasoning: "Amount exceeds the €450 asset threshold, so Equipment is suggested over Office expenses.",
    },
  }),
  inv({
    id: "INV-2051",
    clientId: "c3",
    supplier: "Uitzendbureau Randstad",
    number: "RS-2026-77410",
    invoiceDate: "2026-07-28",
    dueDate: "2026-08-11",
    total: 5412.35,
    vatAmount: 939.29,
    confidence: 45,
    status: "Rejected",
    lines: [line("l1", "Inhuur monteur week 29-30", 74, 61.79)],
    suggestion: {
      ledger: "Subcontracted work",
      vatCode: "NL 21%",
      category: "Personnel costs",
      relation: "Uitzendbureau Randstad",
      reasoning: "Temp agency invoice; period and project allocation could not be determined from the document.",
    },
  }),
  inv({
    id: "INV-2052",
    clientId: "c1",
    supplier: "Verpakkingen Groothandel",
    number: "VG-118240",
    invoiceDate: "2026-08-09",
    dueDate: "2026-09-08",
    total: 398.55,
    vatAmount: 69.16,
    confidence: 96,
    status: "Synced",
    lines: [line("l1", "Broodzakken bedrukt", 5000, 0.066)],
    suggestion: {
      ledger: "Packaging materials",
      vatCode: "NL 21%",
      category: "Cost of goods",
      relation: "Verpakkingen Groothandel",
      reasoning: "Identical article lines booked to Packaging materials in the last 8 invoices.",
    },
  }),
];

export const BANK_TX: BankTx[] = [
  {
    id: "T-8801",
    date: "2026-08-08",
    description: "SEPA overboeking Meelfabriek Van Dijk",
    clientId: "c1",
    amount: -1245.8,
    suggestedInvoiceId: "INV-2041",
    confidence: 96,
    status: "Suggested",
  },
  {
    id: "T-8802",
    date: "2026-08-07",
    description: "Incasso Adobe Systems Europe",
    clientId: "c2",
    amount: -719.4,
    suggestedInvoiceId: "INV-2044",
    confidence: 98,
    status: "Matched",
  },
  {
    id: "T-8803",
    date: "2026-08-07",
    description: "Deelbetaling Technische Unie",
    clientId: "c3",
    amount: -1500.0,
    suggestedInvoiceId: "INV-2045",
    confidence: 74,
    status: "Partial",
    note: "Partial payment — €1,918.75 remaining",
  },
  {
    id: "T-8804",
    date: "2026-08-06",
    description: "Pin-omzet terminal 4 augustus",
    clientId: "c4",
    amount: 2841.15,
    status: "Unmatched",
  },
  {
    id: "T-8805",
    date: "2026-08-05",
    description: "Verzamelbetaling Grolsch Horeca",
    clientId: "c4",
    amount: -1876.4,
    suggestedInvoiceId: "INV-2047",
    confidence: 81,
    status: "Suggested",
    note: "Combined payment — covers 2 invoices",
  },
  {
    id: "T-8806",
    date: "2026-08-04",
    description: "Bankkosten juli 2026",
    clientId: "c1",
    amount: -18.9,
    status: "Ignored",
  },
  {
    id: "T-8807",
    date: "2026-08-03",
    description: "Incasso Eneco Zakelijk",
    clientId: "c4",
    amount: -642.18,
    suggestedInvoiceId: "INV-2048",
    confidence: 88,
    status: "Suggested",
  },
  {
    id: "T-8808",
    date: "2026-08-02",
    description: "Overboeking Drukkerij Veenstra",
    clientId: "c2",
    amount: -468.0,
    suggestedInvoiceId: "INV-2043",
    confidence: 92,
    status: "Matched",
  },
];

export const QUERIES: ClientQuery[] = [
  {
    id: "Q-401",
    clientId: "c3",
    invoiceId: "INV-2046",
    question: "Can you confirm what share of this fuel card statement is business use?",
    createdAt: "2026-08-10",
    status: "Requested",
  },
  {
    id: "Q-402",
    clientId: "c1",
    invoiceId: "INV-2042",
    question: "Was the cooling installation maintenance a repair or a replacement of parts?",
    createdAt: "2026-08-09",
    status: "Delivered",
    answer: "It was regular maintenance, no parts were replaced.",
    answeredAt: "2026-08-10",
  },
  {
    id: "Q-403",
    clientId: "c1",
    invoiceId: "INV-2049",
    question: "We received two identical invoices from Meelfabriek Van Dijk. Did you pay both?",
    createdAt: "2026-08-11",
    status: "Requested",
  },
  {
    id: "Q-404",
    clientId: "c2",
    invoiceId: "INV-2050",
    question: "Are these monitors for the studio or for a client project?",
    createdAt: "2026-08-08",
    status: "Approved",
    answer: "For the studio, they replace the old workstations.",
    answeredAt: "2026-08-09",
  },
  {
    id: "Q-405",
    clientId: "c4",
    question: "Could you send the missing receipt for the terminal takings of 4 August?",
    createdAt: "2026-08-07",
    status: "Delivered",
    answer: "Attached the daily Z-report from the till system.",
    answeredAt: "2026-08-08",
    proof: "z-rapport-04-08.pdf",
  },
];

const now = Date.now();
const mins = (n: number) => now - n * 60_000;

export const ACTIVITY: ActivityEvent[] = [
  {
    id: "a1",
    type: "sync",
    text: "Invoice VG-118240 synced to SnelStart",
    actor: "Abdelouhab",
    at: mins(24),
    clientId: "c1",
    invoiceId: "INV-2052",
  },
  {
    id: "a2",
    type: "answer",
    text: "Client answered a query about cooling maintenance",
    actor: "Bakkerij De Mol",
    at: mins(62),
    clientId: "c1",
    invoiceId: "INV-2042",
  },
  {
    id: "a3",
    type: "approve",
    text: "Invoice GR-2026-3318 approved",
    actor: "Sara",
    at: mins(95),
    clientId: "c4",
    invoiceId: "INV-2047",
  },
  {
    id: "a4",
    type: "ai",
    text: "AI processed invoice CB-4471029 with 84% confidence",
    actor: "Abdelouhab",
    at: mins(140),
    clientId: "c2",
    invoiceId: "INV-2050",
  },
  {
    id: "a5",
    type: "sync-failed",
    text: "Sync failed for EN-90124477 — SnelStart not connected",
    actor: "Abdelouhab",
    at: mins(190),
    clientId: "c4",
    invoiceId: "INV-2048",
  },
  {
    id: "a6",
    type: "upload",
    text: "Document technische-unie-factuur.pdf uploaded",
    actor: "Installatie BV",
    at: mins(260),
    clientId: "c3",
    invoiceId: "INV-2045",
  },
  {
    id: "a7",
    type: "reject",
    text: "Invoice RS-2026-77410 rejected — missing project allocation",
    actor: "Sara",
    at: mins(360),
    clientId: "c3",
    invoiceId: "INV-2051",
  },
  {
    id: "a8",
    type: "query",
    text: "Query sent about fuel card private use",
    actor: "Sara",
    at: mins(420),
    clientId: "c3",
    invoiceId: "INV-2046",
  },
];

export const SETTINGS: Settings = {
  threshold: 85,
  autoSync: false,
  globalRule: "Supplier invoices → default ledger based on historical bookings",
  clientRules: {
    c1: "Flour & packaging suppliers → Raw materials (NL 9%)",
    c2: "Software subscriptions → Software & subscriptions (NL 21%)",
    c3: "Wholesale parts → Materials & parts (NL 21%)",
    c4: "Beverage wholesalers → Beverage purchases (NL 21%)",
  },
};
