// Finance (FRM) data — chart of accounts (fixed), journal, invoices, statements.
// No setup UI: the chart of accounts is configured once and referenced by the app.

const FIN_ACCOUNTS = [
  // Assets
  { code: '1000', name: 'Cash & equivalents', type: 'Asset' },
  { code: '1010', name: 'Accounts Receivable', type: 'Asset' },
  { code: '1200', name: 'Prepaid expenses', type: 'Asset' },
  { code: '1500', name: 'Fixed assets (net)', type: 'Asset' },
  // Liabilities
  { code: '2000', name: 'Accounts Payable', type: 'Liability' },
  { code: '2100', name: 'Deferred revenue', type: 'Liability' },
  { code: '2200', name: 'Credit card payable', type: 'Liability' },
  { code: '2300', name: 'Accrued payroll', type: 'Liability' },
  // Equity
  { code: '3000', name: 'Common stock', type: 'Equity' },
  { code: '3100', name: 'Retained earnings', type: 'Equity' },
  // Revenue
  { code: '4000', name: 'SaaS revenue', type: 'Revenue' },
  { code: '4010', name: 'DaaS revenue', type: 'Revenue' },
  { code: '4020', name: 'Gaming royalties', type: 'Revenue' },
  { code: '4030', name: 'Professional services', type: 'Revenue' },
  // Expenses
  { code: '5000', name: 'Hosting & infrastructure', type: 'Expense' },
  { code: '6000', name: 'Payroll & benefits', type: 'Expense' },
  { code: '6100', name: 'Software & subscriptions', type: 'Expense' },
  { code: '6200', name: 'Marketing & advertising', type: 'Expense' },
  { code: '6300', name: 'Rent & facilities', type: 'Expense' },
  { code: '6400', name: 'Professional fees', type: 'Expense' },
];

// Journal entries (most recent first). Each balances: Σdebits = Σcredits.
const FIN_JOURNAL = [
  { id: 'JE-1042', date: '2026-05-14', ref: 'Payroll', memo: 'Semi-monthly payroll run — May P1', posted: true,
    lines: [ { code: '6000', debit: 184200, credit: 0 }, { code: '2300', debit: 0, credit: 184200 } ] },
  { id: 'JE-1041', date: '2026-05-14', ref: 'INV-2048', memo: 'Stratoscale — May SaaS invoice', posted: true,
    lines: [ { code: '1010', debit: 28400, credit: 0 }, { code: '4000', debit: 0, credit: 28400 } ] },
  { id: 'JE-1040', date: '2026-05-13', ref: 'AWS', memo: 'May cloud hosting — Amex ··7012', posted: true,
    lines: [ { code: '5000', debit: 40000, credit: 0 }, { code: '2200', debit: 0, credit: 40000 } ] },
  { id: 'JE-1039', date: '2026-05-12', ref: 'INV-2045', memo: 'Northbrook — payment received', posted: true,
    lines: [ { code: '1000', debit: 41200, credit: 0 }, { code: '1010', debit: 0, credit: 41200 } ] },
  { id: 'JE-1038', date: '2026-05-10', ref: 'Royalty', memo: 'Steam royalty settlement — April titles', posted: true,
    lines: [ { code: '1000', debit: 62800, credit: 0 }, { code: '4020', debit: 0, credit: 62800 } ] },
  { id: 'JE-1037', date: '2026-05-09', ref: 'Software', memo: 'Monthly SaaS subscriptions batch', posted: true,
    lines: [ { code: '6100', debit: 25434, credit: 0 }, { code: '2200', debit: 0, credit: 25434 } ] },
  { id: 'JE-1036', date: '2026-05-08', ref: 'INV-2043', memo: 'Vellum Health — DaaS invoice', posted: true,
    lines: [ { code: '1010', debit: 12800, credit: 0 }, { code: '4010', debit: 0, credit: 12800 } ] },
  { id: 'JE-1035', date: '2026-05-05', ref: 'Rent', memo: 'Bowling Green HQ — May rent', posted: true,
    lines: [ { code: '6300', debit: 32000, credit: 0 }, { code: '1000', debit: 0, credit: 32000 } ] },
  { id: 'JE-1034', date: '2026-05-03', ref: 'Marketing', memo: 'LinkedIn ABM — Atlas v4 campaign', posted: true,
    lines: [ { code: '6200', debit: 18600, credit: 0 }, { code: '2200', debit: 0, credit: 18600 } ] },
  { id: 'JE-1033', date: '2026-05-01', ref: 'Deferred', memo: 'Recognize May portion of annual prepay', posted: true,
    lines: [ { code: '2100', debit: 22000, credit: 0 }, { code: '4000', debit: 0, credit: 22000 } ] },
  { id: 'JE-1032', date: '2026-05-01', ref: 'Services', memo: 'Junction Bank — implementation milestone 1', posted: false,
    lines: [ { code: '1010', debit: 35000, credit: 0 }, { code: '4030', debit: 0, credit: 35000 } ] },
];

// Invoices (A/R). Amount derived from line items. Today = 2026-05-15.
const FIN_INVOICES = [
  { id: 'INV-2048', customer: 'c1', issued: '2026-05-14', due: '2026-06-13', status: 'Sent',
    lines: [ { desc: 'SaaS platform — May (120 seats)', qty: 1, price: 28400 } ], notes: 'Net 30' },
  { id: 'INV-2047', customer: 'c5', issued: '2026-05-10', due: '2026-06-09', status: 'Sent',
    lines: [ { desc: 'EU data residency — May', qty: 1, price: 22600 } ], notes: 'Net 30 · EUR billed in USD' },
  { id: 'INV-2046', customer: 'c7', issued: '2026-05-08', due: '2026-06-07', status: 'Sent',
    lines: [ { desc: 'DaaS analytics — May', qty: 1, price: 17500 } ], notes: 'Net 30' },
  { id: 'INV-2045', customer: 'c2', issued: '2026-04-15', due: '2026-05-15', status: 'Paid',
    lines: [ { desc: 'SaaS platform — April', qty: 1, price: 41200 } ], notes: 'Paid May 12' },
  { id: 'INV-2044', customer: 'c3', issued: '2026-04-05', due: '2026-05-05', status: 'Overdue',
    lines: [ { desc: 'DaaS — April (320 seats)', qty: 1, price: 12800 } ], notes: '10 days overdue' },
  { id: 'INV-2043', customer: 'c6', issued: '2026-03-20', due: '2026-04-19', status: 'Overdue',
    lines: [ { desc: 'District onboarding + seats', qty: 1, price: 3800 } ], notes: '26 days overdue' },
  { id: 'INV-2042', customer: 'c8', issued: '2026-02-28', due: '2026-03-30', status: 'Overdue',
    lines: [ { desc: 'Pilot SOW — milestone 1', qty: 1, price: 35000 } ], notes: '46 days overdue · escalated' },
  { id: 'INV-2041', customer: 'c4', issued: '2026-05-04', due: '2026-06-03', status: 'Draft',
    lines: [ { desc: 'Team plan — annual', qty: 1, price: 14400 } ], notes: 'Awaiting PO' },
];

// Tie each invoice line to a revenue account in the chart of accounts.
FIN_INVOICES.forEach(inv => inv.lines.forEach(l => {
  if (!l.code) {
    const d = l.desc.toLowerCase();
    l.code = d.includes('daas') || d.includes('data') ? '4010'
      : (d.includes('service') || d.includes('integration') || d.includes('setup') || d.includes('jumpstart') || d.includes('onboard')) ? '4030'
      : (d.includes('royalt') || d.includes('game')) ? '4020'
      : '4000';
  }
}));

// Period statement figures (May 2026) — coherent with the activity above.
const FIN_PNL = {
  period: 'May 2026',
  revenue: [
    { name: 'SaaS revenue', amount: 318000 },
    { name: 'DaaS revenue', amount: 96000 },
    { name: 'Gaming royalties', amount: 62800 },
    { name: 'Professional services', amount: 35000 },
  ],
  cogs: [
    { name: 'Hosting & infrastructure', amount: 48000 },
    { name: 'Payment processing', amount: 9400 },
  ],
  opex: [
    { name: 'Payroll & benefits', amount: 368400 },
    { name: 'Software & subscriptions', amount: 25434 },
    { name: 'Marketing & advertising', amount: 38600 },
    { name: 'Rent & facilities', amount: 32000 },
    { name: 'Professional fees', amount: 14000 },
  ],
  prior: { revenue: 472000, netIncome: -8200 }, // April comparison
};

const FIN_BALANCE = {
  asOf: '2026-05-31',
  assets: [
    { name: 'Cash & equivalents', amount: 1284600 },
    { name: 'Accounts Receivable', amount: 159300 },
    { name: 'Prepaid expenses', amount: 84000 },
    { name: 'Fixed assets (net)', amount: 212000 },
  ],
  liabilities: [
    { name: 'Accounts Payable', amount: 96400 },
    { name: 'Deferred revenue', amount: 418000 },
    { name: 'Credit card payable', amount: 84034 },
    { name: 'Accrued payroll', amount: 184200 },
  ],
  equity: [
    { name: 'Common stock', amount: 500000 },
    { name: 'Retained earnings', amount: 457266 },
  ],
};

window.HitcentsData.FIN_ACCOUNTS = FIN_ACCOUNTS;
window.HitcentsData.FIN_JOURNAL = FIN_JOURNAL;
window.HitcentsData.FIN_INVOICES = FIN_INVOICES;
window.HitcentsData.FIN_PNL = FIN_PNL;
window.HitcentsData.FIN_BALANCE = FIN_BALANCE;

// Account ending balances (for trial balance & ledger). Debits = credits.
const FIN_BALANCES = {
  '1000': 1284600, '1010': 159300, '1200': 84000, '1500': 212000,
  '2000': 96400, '2100': 418000, '2200': 84034, '2300': 184200,
  '3000': 500000, '3100': 471900,
  '4000': 318000, '4010': 96000, '4020': 62800, '4030': 35000,
  '5000': 48000, '6000': 368400, '6100': 25434, '6200': 38600, '6300': 32000, '6400': 14000,
};

// Bills / payables (A/P). Today = 2026-05-15.
const FIN_BILLS = [
  { id: 'BILL-7012', vendor: 'Amazon Web Services',    category: 'Hosting & infrastructure', issued: '2026-05-13', due: '2026-06-12', amount: 40000, status: 'Open' },
  { id: 'BILL-7011', vendor: 'Datadog Inc.',           category: 'Software & subscriptions', issued: '2026-05-09', due: '2026-06-08', amount: 8000,  status: 'Open' },
  { id: 'BILL-7010', vendor: 'PEER Audit Co.',         category: 'Professional fees',        issued: '2026-04-10', due: '2026-05-10', amount: 7000,  status: 'Overdue' },
  { id: 'BILL-7009', vendor: 'Greenbriar Properties',  category: 'Rent & facilities',        issued: '2026-05-01', due: '2026-05-05', amount: 32000, status: 'Paid' },
  { id: 'BILL-7008', vendor: 'LinkedIn Marketing',     category: 'Marketing & advertising',  issued: '2026-05-03', due: '2026-06-02', amount: 18600, status: 'Open' },
  { id: 'BILL-7007', vendor: 'Adobe Inc.',             category: 'Software & subscriptions', issued: '2026-04-28', due: '2026-05-28', amount: 1200,  status: 'Open' },
  { id: 'BILL-7006', vendor: 'Linear Orbit, Inc.',     category: 'Software & subscriptions', issued: '2026-04-15', due: '2026-04-30', amount: 1200,  status: 'Paid' },
  { id: 'BILL-7005', vendor: 'Greenbriar Properties',  category: 'Rent & facilities',        issued: '2026-03-01', due: '2026-04-04', amount: 32000, status: 'Overdue' },
];

// Tie each seeded bill to a vendor + a chart-of-accounts allocation.
(() => {
  const catCode = {
    'Hosting & infrastructure': '5000', 'Software & subscriptions': '6100',
    'Payroll & benefits': '6000', 'Marketing & advertising': '6200',
    'Rent & facilities': '6300', 'Professional fees': '6400',
  };
  const vendors = window.HitcentsData.VENDORS || [];
  FIN_BILLS.forEach(b => {
    if (!b.splits) b.splits = [{ code: catCode[b.category] || '6100', amount: b.amount }];
    if (b.vendorId === undefined) {
      const v = vendors.find(v => (v.aliases || [v.name]).some(a => b.vendor.toLowerCase().includes(a.toLowerCase().split(' ')[0])));
      b.vendorId = v ? v.id : null;
    }
    if (!b.files) b.files = [{ name: `${b.id} — ${b.vendor.split(' ')[0]} invoice.pdf`, size: 180000 + (b.amount % 7) * 20000 }];
  });
})();

// Bank / cash accounts + transaction register.
const FIN_BANK_ACCOUNTS = [
  { id: 'ba1', name: 'Operating Checking', bank: 'Amex Business', last4: '4021', balance: 1100000, type: 'Checking' },
  { id: 'ba2', name: 'Money Market Reserve', bank: 'Amex Business', last4: '9088', balance: 184600, type: 'Savings' },
  { id: 'ba3', name: 'Corporate Card', bank: 'Brex', last4: '9930', balance: -84034, type: 'Credit card' },
];

const FIN_TRANSACTIONS = [
  { id: 't1', date: '2026-05-14', desc: 'Stripe payout — customer invoices', account: 'ba1', amount: 41200,  category: 'Accounts Receivable', reconciled: true },
  { id: 't2', date: '2026-05-14', desc: 'ADP payroll run — May P1',          account: 'ba1', amount: -184200, category: 'Payroll & benefits', reconciled: true },
  { id: 't3', date: '2026-05-13', desc: 'AWS — May cloud hosting',           account: 'ba3', amount: -40000,  category: 'Hosting & infrastructure', reconciled: true },
  { id: 't4', date: '2026-05-12', desc: 'Steam royalty settlement',          account: 'ba1', amount: 62800,   category: 'Gaming royalties', reconciled: true },
  { id: 't5', date: '2026-05-10', desc: 'SaaS subscriptions batch',          account: 'ba3', amount: -25434,  category: 'Software & subscriptions', reconciled: false },
  { id: 't6', date: '2026-05-08', desc: 'Vellum Health — invoice payment',   account: 'ba1', amount: 12800,   category: 'Accounts Receivable', reconciled: false },
  { id: 't7', date: '2026-05-05', desc: 'Greenbriar — May rent',             account: 'ba1', amount: -32000,  category: 'Rent & facilities', reconciled: true },
  { id: 't8', date: '2026-05-03', desc: 'LinkedIn ABM — Atlas v4',           account: 'ba3', amount: -18600,  category: 'Marketing & advertising', reconciled: false },
  { id: 't9', date: '2026-05-01', desc: 'Transfer to Money Market',          account: 'ba1', amount: -50000,  category: 'Transfer', reconciled: true },
  { id: 't10', date: '2026-05-01', desc: 'Transfer from Operating',          account: 'ba2', amount: 50000,   category: 'Transfer', reconciled: true },
];

// Cash flow statement (May 2026, indirect method).
const FIN_CASHFLOW = {
  period: 'May 2026',
  operating: [
    { name: 'Net income', amount: -24034 },
    { name: 'Depreciation & amortization', amount: 11000 },
    { name: 'Increase in accounts receivable', amount: -18300 },
    { name: 'Increase in deferred revenue', amount: 42000 },
    { name: 'Increase in accounts payable', amount: 14600 },
  ],
  investing: [
    { name: 'Purchase of equipment', amount: -8200 },
  ],
  financing: [
    { name: 'Repayment of credit line', amount: 0 },
  ],
  beginningCash: 1259734,
};

window.HitcentsData.FIN_BALANCES = FIN_BALANCES;
window.HitcentsData.FIN_BILLS = FIN_BILLS;
window.HitcentsData.FIN_BANK_ACCOUNTS = FIN_BANK_ACCOUNTS;
window.HitcentsData.FIN_TRANSACTIONS = FIN_TRANSACTIONS;
window.HitcentsData.FIN_CASHFLOW = FIN_CASHFLOW;

window.finAccount = (code) => FIN_ACCOUNTS.find(a => a.code === code);
window.invoiceTotal = (inv) => inv.lines.reduce((s, l) => s + l.qty * l.price, 0);
