// Demo timeline — defines all scenes for the auto-play 90s walkthrough
// Each scene has: t (start), label, route, callout (annotation), cursorTarget, etc.
//
// Audience: distributors / managers evaluating Better Inventory.
// Voice: plain language. No SQL, no row-locks, no idempotency talk.
// Story:
//   1. Manager opens dashboard — sees 4 things waiting.
//   2. Flashback to morning — operator submitted a receipt from his phone.
//   3. Manager opens that receipt, sees what stock will move, approves.
//   4. Stock moves. Ledger appends. Audit trail records who-did-what-when.
//   5. Outro.

// Total ~90 seconds
const DEMO_DURATION = 90;

const SCENES = [
  // ─── Scene 1 — Title (0–4s)
  {
    t: 0, label: "Open the dashboard",
    route: "/dashboard",
    persona: "manager",
    title: { line1: "Better Inventory", line2: "Approve. Apply. Audit." },
  },
  // ─── Scene 2 — Dashboard tour (4–14s)
  {
    t: 4, label: "Vouchers waiting on the manager",
    route: "/dashboard",
    persona: "manager",
    callout: { target: "approvals-card", side: "top",
      title: "4 vouchers waiting",
      body: "Operators submit from the floor. Stock only moves when the manager approves." },
  },
  {
    t: 9, label: "Spot the receipt to approve",
    route: "/dashboard",
    persona: "manager",
    cursor: { target: "approval-GRN-2026-0184" },
    highlight: "GRN-2026-0184",
    callout: { target: "approval-GRN-2026-0184", side: "right",
      title: "GRN-2026-0184",
      body: "A Britannia delivery — Ravi submitted it from his phone earlier this morning." },
  },
  // ─── Scene 3 — Mobile operator submission (14–30s, +2s for breathing room)
  {
    t: 14, label: "Earlier today, on the floor",
    page: "mobile",
    mobileScene: "list",
    persona: "operator",
    title: { line1: "Earlier this morning", line2: "Ravi unloads the Britannia delivery" },
  },
  {
    t: 19, label: "Operator opens a new receipt",
    page: "mobile",
    mobileScene: "list",
    persona: "operator",
    cursor: { target: "mob-create" },
    callout: { target: "mob-create", side: "bottom",
      title: "Tap to start a new receipt",
      body: "Operator opens a new GRN and snaps a photo of the supplier’s invoice." },
  },
  {
    t: 23, label: "OCR fills in the lines",
    page: "mobile",
    mobileScene: "review",
    persona: "operator",
    callout: { target: "mob-submit", side: "top",
      title: "Lines auto-filled from the invoice",
      body: "Britannia Bourbon, Parle Oil, Taj Tea — quantities, rates, GST. Every field above 95% confidence." },
  },
  {
    t: 28, label: "Submit",
    page: "mobile",
    mobileScene: "submitted",
    persona: "operator",
    callout: { target: null, side: "center",
      title: "Sent for approval",
      body: "Operators never move stock directly. The voucher waits in the manager’s queue until reviewed." },
  },
  // ─── Scene 4 — Manager opens approvals (32–42s)
  {
    t: 32, label: "Back at the manager’s desk",
    route: "/approvals",
    persona: "manager",
    title: { line1: "Back at the manager’s desk", line2: "Priya reviews the queue" },
  },
  {
    t: 36, label: "Open the receipt",
    route: "/approvals",
    persona: "manager",
    cursor: { target: "approval-row-GRN-2026-0184" },
    highlight: "GRN-2026-0184",
    openDetail: "GRN-2026-0184",
  },
  {
    t: 39, label: "What this voucher will do to stock",
    route: "/approvals",
    persona: "manager",
    openDetail: "GRN-2026-0184",
    callout: { target: "voucher-detail", side: "left",
      title: "See the impact before you approve",
      body: "Three lines, 660 units. Linked to PO-2026-0091. Submitted from Telegram, with the original invoice attached." },
  },
  // ─── Scene 5 — The approve action (43–52s)
  {
    t: 43, label: "Approve",
    route: "/approvals",
    persona: "manager",
    openDetail: "GRN-2026-0184",
    cursor: { target: "btn-approve" },
    callout: { target: "btn-approve", side: "left",
      title: "One click",
      body: "Approve and the stock moves. Reject and nothing changes — every decision is recorded." },
  },
  {
    t: 46, label: "Approving",
    route: "/approvals",
    persona: "manager",
    openDetail: "GRN-2026-0184",
    isApproving: true,
  },
  {
    t: 48, label: "Approved",
    route: "/approvals",
    persona: "manager",
    openDetail: "GRN-2026-0184",
    isApproved: true,
    callout: { target: "voucher-detail", side: "left",
      title: "Stock applied",
      body: "Three items moved into MUM-1. The voucher is locked — any change from here is a new entry, not an edit." },
  },
  // ─── Scene 6 — Stock Levels updates live (53–62s)
  {
    t: 53, label: "Stock Levels",
    route: "/stock",
    persona: "manager",
    title: { line1: "Stock updates instantly", line2: "Across every location" },
  },
  {
    t: 57, label: "New quantities land",
    route: "/stock",
    persona: "manager",
    stockAfter: true,
    deltaIds: ["itm-001-loc-mum-1", "itm-002-loc-mum-1", "itm-004-loc-mum-1"],
    callout: { target: "stock-row-itm-001-loc-mum-1", side: "top",
      title: "+480 Britannia Bourbon",
      body: "96 → 576. Taj Tea is no longer below its reorder level either." },
  },
  // ─── Scene 7 — Stock Ledger append (63–73s)
  {
    t: 63, label: "Stock Ledger",
    route: "/ledger",
    persona: "manager",
    title: { line1: "Every movement, on the record", line2: "The stock ledger" },
  },
  {
    t: 67, label: "New row, never an edit",
    route: "/ledger",
    persona: "manager",
    ledgerWithNew: true,
    highlightLedgerNew: true,
    callout: { target: "ledger-row-L-1083", side: "top",
      title: "New row appended",
      body: "L-1083 records +480 with the running balance. Mistakes are corrected with reversal rows — the original is never deleted." },
  },
  // ─── Scene 8 — Audit log (74–83s)
  {
    t: 74, label: "Audit log",
    route: "/audit",
    persona: "manager",
    auditHighlightFirst: true,
    callout: { target: "audit-table", side: "top",
      title: "Who did what, when",
      body: "Every action is logged: Ravi’s submission from Telegram, Priya’s approval from the web. Forever, never pruned." },
  },
  // ─── Scene 9 — Outro (83–90s)
  {
    t: 83, label: "Outro",
    route: "/dashboard",
    persona: "manager",
    kpisAfter: true,
    title: { line1: "Better Inventory", line2: "Approve. Apply. Audit." },
  },
];

// Get the scene that's currently active for a given t
function sceneAt(t) {
  let cur = SCENES[0];
  for (const s of SCENES) {
    if (t >= s.t) cur = s; else break;
  }
  return cur;
}

// Compute progress within the current scene
function sceneProgress(t) {
  let i = 0;
  for (let k = 0; k < SCENES.length; k++) {
    if (t >= SCENES[k].t) i = k; else break;
  }
  const cur = SCENES[i];
  const next = SCENES[i + 1];
  const end = next ? next.t : DEMO_DURATION;
  const dur = end - cur.t;
  const k = dur > 0 ? Math.min(1, (t - cur.t) / dur) : 1;
  return { scene: cur, next, k, idx: i };
}

Object.assign(window, { SCENES, DEMO_DURATION, sceneAt, sceneProgress });
