// Demo data for Sharma Distributors
// Items, locations, vouchers, ledger rows, audit log

const TENANT = {
  name: "Sharma Distributors",
  slug: "sharma-distributors",
  city: "Mumbai",
  fy: "FY 25-26",
};

const USER_MANAGER = { name: "Priya Sharma", role: "Manager", initials: "PS" };
const USER_OPERATOR = { name: "Ravi Kumar", role: "Operator", initials: "RK" };

const LOCATIONS = [
  { id: "loc-mum-1", code: "MUM-1", name: "Mumbai Central Warehouse", type: "Warehouse" },
  { id: "loc-mum-2", code: "MUM-2", name: "Andheri Godown",            type: "Godown" },
  { id: "loc-pun-1", code: "PUN-1", name: "Pune Distribution Centre",  type: "Warehouse" },
  { id: "loc-shop", code: "SHOP",  name: "Dadar Retail Shop",          type: "Shop" },
];

const ITEMS = [
  { id: "itm-001", sku: "BSC-PB-200",  name: "Britannia Bourbon 200g",      unit: "pcs", altUnit: "box (24)", gst: 18, reorder: 240, price: 32, cost: 22 },
  { id: "itm-002", sku: "PSL-RFN-1L",  name: "Parle Refined Oil 1L",        unit: "ltr", altUnit: "ctn (12)", gst: 5,  reorder: 120, price: 145, cost: 118 },
  { id: "itm-003", sku: "AMC-DST-25",  name: "Amul Distilled Water 25L",    unit: "ltr", altUnit: null,        gst: 18, reorder: 80,  price: 55,  cost: 38 },
  { id: "itm-004", sku: "TAJ-HRB-500", name: "Taj Mahal Tea Herbal 500g",   unit: "pcs", altUnit: "box (10)", gst: 12, reorder: 60,  price: 220, cost: 162 },
  { id: "itm-005", sku: "MRD-DET-2K",  name: "Surf Excel Detergent 2kg",    unit: "pcs", altUnit: "ctn (6)",  gst: 18, reorder: 90,  price: 425, cost: 320 },
  { id: "itm-006", sku: "DTL-RCE-25K", name: "Daawat Basmati Rice 25kg",    unit: "kg",  altUnit: null,        gst: 5,  reorder: 200, price: 78,  cost: 56 },
  { id: "itm-007", sku: "MRC-CHL-1K",  name: "MDH Chilli Powder 1kg",       unit: "kg",  altUnit: "box (12)", gst: 12, reorder: 50,  price: 280, cost: 198 },
  { id: "itm-008", sku: "HLD-BSN-5K",  name: "Haldiram Besan Flour 5kg",    unit: "kg",  altUnit: null,        gst: 5,  reorder: 100, price: 92,  cost: 64 },
];

const SUPPLIERS = [
  { id: "sup-001", name: "Britannia Industries Ltd",    gstin: "27AABCB1234M1Z5", city: "Mumbai" },
  { id: "sup-002", name: "Parle Products Pvt Ltd",      gstin: "27AAACP4567L1ZA", city: "Mumbai" },
  { id: "sup-003", name: "ITC Foods & Beverages",       gstin: "33AAACI5678N1ZQ", city: "Chennai" },
  { id: "sup-004", name: "Mahesh Wholesale Supplies",   gstin: "27ABCDE3456F1ZX", city: "Pune" },
];

// Stock levels — what the Stock Levels page shows.
// before/after pair will animate during the demo when a receipt is approved.
const STOCK_BEFORE = [
  { itemId: "itm-001", locId: "loc-mum-1", qty: 96  },
  { itemId: "itm-001", locId: "loc-mum-2", qty: 48  },
  { itemId: "itm-002", locId: "loc-mum-1", qty: 84  },
  { itemId: "itm-003", locId: "loc-mum-1", qty: 320 },
  { itemId: "itm-004", locId: "loc-mum-1", qty: 28  }, // low
  { itemId: "itm-005", locId: "loc-mum-1", qty: 142 },
  { itemId: "itm-006", locId: "loc-pun-1", qty: 180 },
  { itemId: "itm-007", locId: "loc-mum-1", qty: 22  }, // low
  { itemId: "itm-008", locId: "loc-mum-1", qty: 72  },
];

// After approval: GRN-2026-0184 receipts — Britannia Bourbon +480pcs, Parle Oil +60ltr, Taj Tea +120pcs into MUM-1
const RECEIPT_LINES = [
  { itemId: "itm-001", locId: "loc-mum-1", qty: 480, rate: 22, unit: "pcs (20 box × 24)" },
  { itemId: "itm-002", locId: "loc-mum-1", qty: 60,  rate: 118, unit: "ltr (5 ctn × 12)" },
  { itemId: "itm-004", locId: "loc-mum-1", qty: 120, rate: 162, unit: "pcs (12 box × 10)" },
];

const STOCK_AFTER = STOCK_BEFORE.map(r => {
  const add = RECEIPT_LINES.find(l => l.itemId === r.itemId && l.locId === r.locId);
  return add ? { ...r, qty: r.qty + add.qty } : r;
});

// Existing ledger rows for itm-001 (Britannia Bourbon) at MUM-1
const LEDGER_BEFORE = [
  { id: "L-1041", date: "28 Apr 2026", voucher: "GRN-2026-0178", type: "Receipt",  delta:  +240, balance:  240, party: "Britannia Industries", note: "Opening dispatch" },
  { id: "L-1058", date: "30 Apr 2026", voucher: "DSP-2026-0312", type: "Dispatch",  delta:  -96,  balance:  144, party: "Apna Bazaar Dadar",    note: "Order #4421" },
  { id: "L-1066", date: "02 May 2026", voucher: "DSP-2026-0319", type: "Dispatch",  delta:  -48,  balance:   96,  party: "Reliance Smart, Worli", note: "Order #4438" },
];

// New ledger row written by the approval — appended, never updated.
const LEDGER_NEW = { id: "L-1083", date: "07 May 2026", voucher: "GRN-2026-0184", type: "Receipt", delta: +480, balance: 576, party: "Britannia Industries", note: "PO-2026-0091 fulfilled" };

const AUDIT_LOG = [
  { ts: "07 May 2026, 11:42:08", actor: "Priya Sharma",  action: "voucher.approve",   target: "GRN-2026-0184", origin: "web_clerk",     meta: "status: pending_review → open · 3 lines · MUM-1" },
  { ts: "07 May 2026, 09:17:42", actor: "Ravi Kumar",    action: "voucher.create",    target: "GRN-2026-0184", origin: "telegram_webapp",meta: "status: pending_review · linked PO-2026-0091" },
  { ts: "07 May 2026, 11:18:24", actor: "Priya Sharma",  action: "voucher.approve",   target: "GRN-2026-0183", origin: "web_clerk",     meta: "status: pending_review → open · 5 lines · MUM-2" },
  { ts: "07 May 2026, 10:54:02", actor: "Ravi Kumar",    action: "voucher.create",    target: "GRN-2026-0183", origin: "telegram_webapp",meta: "status: pending_review · 5 lines · MUM-2" },
  { ts: "07 May 2026, 10:21:45", actor: "Priya Sharma",  action: "item.update",       target: "BSC-PB-200",    origin: "web_clerk",     meta: "reorder: 200 → 240" },
  { ts: "07 May 2026, 09:48:11", actor: "Anil Sharma",   action: "voucher.cancel",    target: "DSP-2026-0316", origin: "web_clerk",     meta: "reversal rows added · reason: customer cancel" },
  { ts: "07 May 2026, 09:32:00", actor: "system",        action: "tally.import",      target: "TLY-IMP-2026-0019", origin: "web_clerk", meta: "items: 412 ok / 3 err · contacts: 88 ok" },
  { ts: "06 May 2026, 18:17:33", actor: "Priya Sharma",  action: "user.invite",       target: "rohan@sharma-co.in", origin: "web_clerk",meta: "role: operator · scope: MUM-2" },
];

// Receipt value — single source of truth, derived from the lines.
// Britannia 480 × ₹22 + Parle 60 × ₹118 + Taj 120 × ₹162 = ₹37,080
const GRN_184_VALUE = RECEIPT_LINES.reduce((s, l) => s + l.qty * l.rate, 0);

// KPIs
const KPIS_BEFORE = {
  stockValue: 4827400,    // ₹48.27L
  pendingApprovals: 4,
  lowStockItems: 2,       // matches what's actually visible in STOCK_BEFORE (itm-004 28<60, itm-007 22<50)
  todayInward: 1,         // GRN-2026-0182 approved earlier today
  todayOutward: 5,
};

const KPIS_AFTER = {
  stockValue: 4827400 + GRN_184_VALUE,
  pendingApprovals: 3,
  lowStockItems: 1,       // Taj Tea no longer low; MDH Chilli still is
  todayInward: 2,         // 0182 + 0184
  todayOutward: 5,
};

// Pending approvals queue
const PENDING_APPROVALS = [
  { id: "GRN-2026-0184", type: "Receipt",  party: "Britannia Industries", lines: 3, value: GRN_184_VALUE,  by: "Ravi Kumar",   loc: "MUM-1", at: "09:17 AM" },
  { id: "GRN-2026-0185", type: "Receipt",  party: "Parle Products",       lines: 2, value: 18420,  by: "Suresh M.",    loc: "MUM-2", at: "10:14 AM" },
  { id: "DSP-2026-0322", type: "Dispatch", party: "Apna Bazaar Dadar",    lines: 7, value: 24850,  by: "Ravi Kumar",   loc: "MUM-1", at: "09:51 AM" },
  { id: "ADJ-2026-0014", type: "Adjustment", party: "—",                  lines: 1, value: -2400,   by: "Suresh M.",    loc: "MUM-2", at: "Yesterday" },
];

// Activity feed
const ACTIVITY = [
  { kind: "approve",  who: "Priya",  what: "approved GRN-2026-0183",  when: "12 min ago" },
  { kind: "create",   who: "Ravi",   what: "created GRN-2026-0184",   when: "2 hr ago" },
  { kind: "dispatch", who: "Anil",   what: "dispatched DSP-2026-0321", when: "3 hr ago" },
  { kind: "low",      who: "system", what: "MDH Chilli Powder is low", when: "2 hr ago" },
];

// Helper
const fmtINR = (n) => {
  if (n == null) return "—";
  const sign = n < 0 ? "-" : "";
  const x = Math.abs(n).toLocaleString("en-IN");
  return `${sign}₹${x}`;
};
const fmtINRCompact = (n) => {
  if (n >= 10000000) return `₹${(n/10000000).toFixed(2)} Cr`;
  if (n >= 100000)   return `₹${(n/100000).toFixed(2)} L`;
  if (n >= 1000)     return `₹${(n/1000).toFixed(1)}k`;
  return `₹${n}`;
};
const itemById = (id) => ITEMS.find(i => i.id === id);
const locById  = (id) => LOCATIONS.find(l => l.id === id);

const LOW_STOCK_ITEMS = [
  { sku: "MDH-CHP-100", name: "MDH Chilli Powder 100g", qty: 18, reorder: 80 },
  { sku: "PSL-RFN-1L",  name: "Parle Refined Oil 1L",   qty: 42, reorder: 120 },
  { sku: "BSC-PB-200",  name: "Britannia Bourbon 200g", qty: 96, reorder: 240 },
  { sku: "TTL-SLT-1K",  name: "Tata Salt 1kg",          qty: 110, reorder: 200 },
];

Object.assign(window, {
  TENANT, USER_MANAGER, USER_OPERATOR,
  LOCATIONS, ITEMS, SUPPLIERS,
  STOCK_BEFORE, STOCK_AFTER, RECEIPT_LINES, GRN_184_VALUE,
  LEDGER_BEFORE, LEDGER_NEW,
  AUDIT_LOG,
  KPIS_BEFORE, KPIS_AFTER, PENDING_APPROVALS, ACTIVITY,
  LOW_STOCK_ITEMS,
  fmtINR, fmtINRCompact, itemById, locById,
});
