/* === Demo state === Loaded when a visitor clicks "View demo" on the sign-in
   page. Generates dates relative to TODAY so the demo always looks fresh.
   In-memory only — never written to Supabase. */

function buildDemoState() {
  const todayIso = isoDate(TODAY);
  const offset = (n) => addDaysIso(todayIso, n);
  const todayMonth = currentMonthKey();
  const lastMonth = shiftMonth(todayMonth, -1);
  const twoMonthsAgo = shiftMonth(todayMonth, -2);

  // Anchor expense dates to the current month so the demo always shows a
  // populated current-month view regardless of when someone visits.
  const dayInMonth = (key, day) => {
    const d = Math.min(day, daysInMonthKey(key));
    return `${key}-${String(d).padStart(2, '0')}`;
  };

  const tags = [
    { id: 'food',    name: 'Groceries',     color: 'moss',       budget: 600 },
    { id: 'dining',  name: 'Dining',        color: 'terracotta', budget: 220 },
    { id: 'transit', name: 'Transit',       color: 'slate',      budget: 80  },
    { id: 'fuel',    name: 'Fuel',          color: 'ink',        budget: 160 },
    { id: 'home',    name: 'Home',          color: 'sand',       budget: 200 },
    { id: 'health',  name: 'Health',        color: 'plum',       budget: 100 },
    { id: 'fun',     name: 'Entertainment', color: 'ochre',      budget: 140 },
    { id: 'gift',    name: 'Gifts',         color: 'rose',       budget: 60  },
    { id: 'subs',    name: 'Subscriptions', color: 'teal',       budget: 50  },
    { id: 'work',    name: 'Work',          color: 'olive',      budget: 0   },
  ];

  const family = [
    { id: 'self',  name: 'John Smith',  initials: 'J', color: 'slate',      budget: 0, role: 'primary' },
    { id: 'sarah', name: 'Sarah Smith', initials: 'S', color: 'terracotta', budget: 0 },
  ];

  // ~14 expenses for the current month, plus a handful in prior months
  // so the stats screen and sparklines show a real shape.
  const expenses = [
    // Current month
    { id: 'd-e1',  date: dayInMonth(todayMonth, 2),  amount: 87.43,  description: 'Whole Foods',     tagId: 'food',    memberId: 'sarah' },
    { id: 'd-e2',  date: dayInMonth(todayMonth, 3),  amount: 4.95,   description: 'Morning coffee',  tagId: 'dining',  memberId: 'self'  },
    { id: 'd-e3',  date: dayInMonth(todayMonth, 5),  amount: 52.10,  description: 'Shell',            tagId: 'fuel',    memberId: 'self'  },
    { id: 'd-e4',  date: dayInMonth(todayMonth, 6),  amount: 62.18,  description: "Trader Joe's",    tagId: 'food',    memberId: 'sarah' },
    { id: 'd-e5',  date: dayInMonth(todayMonth, 8),  amount: 48.20,  description: 'Olive Garden',    tagId: 'dining',  memberId: 'self'  },
    { id: 'd-e6',  date: dayInMonth(todayMonth, 10), amount: 17.99,  description: 'Spotify family',  tagId: 'subs',    memberId: 'self'  },
    { id: 'd-e7',  date: dayInMonth(todayMonth, 11), amount: 158.20, description: 'Costco run',      tagId: 'food',    memberId: 'sarah' },
    { id: 'd-e8',  date: dayInMonth(todayMonth, 13), amount: 34.99,  description: 'Hardware store',  tagId: 'home',    memberId: 'self'  },
    { id: 'd-e9',  date: dayInMonth(todayMonth, 15), amount: 22.40,  description: 'Pharmacy',        tagId: 'health',  memberId: 'sarah' },
    { id: 'd-e10', date: dayInMonth(todayMonth, 17), amount: 32.00,  description: 'Movie night',     tagId: 'fun',     memberId: 'self'  },
    { id: 'd-e11', date: dayInMonth(todayMonth, 18), amount: 45.00,  description: "Mom's birthday",  tagId: 'gift',    memberId: 'sarah' },
    { id: 'd-e12', date: dayInMonth(todayMonth, 20), amount: 48.65,  description: 'Sunoco',           tagId: 'fuel',    memberId: 'self'  },
    { id: 'd-e13', date: dayInMonth(todayMonth, 22), amount: 15.49,  description: 'Netflix',          tagId: 'subs',    memberId: 'self'  },
    { id: 'd-e14', date: dayInMonth(todayMonth, 24), amount: 71.30,  description: 'Whole Foods',     tagId: 'food',    memberId: 'sarah' },
    // Last month — for stats history
    { id: 'd-e15', date: dayInMonth(lastMonth, 4),  amount: 92.10,  description: 'Whole Foods',     tagId: 'food',    memberId: 'sarah' },
    { id: 'd-e16', date: dayInMonth(lastMonth, 9),  amount: 178.50, description: 'Costco run',      tagId: 'food',    memberId: 'sarah' },
    { id: 'd-e17', date: dayInMonth(lastMonth, 14), amount: 58.00,  description: 'Date night',      tagId: 'dining',  memberId: 'self'  },
    { id: 'd-e18', date: dayInMonth(lastMonth, 22), amount: 49.20,  description: 'Shell',            tagId: 'fuel',    memberId: 'self'  },
    // Two months ago
    { id: 'd-e19', date: dayInMonth(twoMonthsAgo, 6),  amount: 110.40, description: 'Whole Foods', tagId: 'food', memberId: 'sarah' },
    { id: 'd-e20', date: dayInMonth(twoMonthsAgo, 19), amount: 64.00,  description: 'Brunch',       tagId: 'dining', memberId: 'sarah' },
  ];

  const fixed = [
    { id: 'd-f1', name: 'Rent',           amount: 2200, frequency: 'monthly', tagId: 'home',  dueDay: 1  },
    { id: 'd-f2', name: 'Internet',       amount: 79,   frequency: 'monthly', tagId: 'home',  dueDay: 5  },
    { id: 'd-f3', name: 'Car insurance',  amount: 145,  frequency: 'monthly', tagId: 'transit', dueDay: 12 },
    { id: 'd-f4', name: 'Phone plan',     amount: 90,   frequency: 'monthly', tagId: 'subs',  dueDay: 18 },
    { id: 'd-f5', name: 'Gym',            amount: 38,   frequency: 'monthly', tagId: 'health', dueDay: 22 },
  ];

  const goals = [
    { id: 'd-g1', name: 'Hawaii trip',    target: 5000,  saved: 1850, dueDate: shiftMonth(todayMonth, 5) + '-01', color: 'teal' },
    { id: 'd-g2', name: 'Emergency fund', target: 15000, saved: 8200, dueDate: shiftMonth(todayMonth, 12) + '-01', color: 'moss' },
  ];

  const taskGroups = [
    { id: 'd-tg1', name: 'Home',   color: 'sand' },
    { id: 'd-tg2', name: 'Car',    color: 'slate' },
    { id: 'd-tg3', name: 'Errands', color: 'ochre' },
  ];

  const tasks = [
    { id: 'd-t1', title: 'Pick up dry cleaning', dueDate: todayIso,    memberId: 'sarah', groupId: 'd-tg3', done: false, subtasks: [] },
    { id: 'd-t2', title: 'Replace air filter',   dueDate: offset(2),   memberId: 'self',  groupId: 'd-tg1', done: false, subtasks: [] },
    { id: 'd-t3', title: 'Schedule oil change',  dueDate: offset(4),   memberId: 'self',  groupId: 'd-tg2', done: false, subtasks: [] },
    { id: 'd-t4', title: 'Renew passport',       dueDate: offset(14),  memberId: 'self',  groupId: 'd-tg3', done: false,
      note: 'Need passport photos first.',
      subtasks: [
        { id: 'd-st1', title: 'Get passport photos', memberId: 'self',  done: false },
        { id: 'd-st2', title: 'Submit application',   memberId: 'self',  done: false },
      ]
    },
    { id: 'd-t5', title: 'Plan summer vacation',                       memberId: 'sarah', groupId: null,    done: false, subtasks: [] },
    { id: 'd-t6', title: 'Take out trash',       dueDate: offset(-1),  memberId: 'self',  groupId: 'd-tg1', done: true, subtasks: [] },
  ];

  const recipes = [
    {
      id: 'd-r1', name: 'Chicken stir-fry', mealTags: ['dinner'], portions: 4, leftovers: true,
      ingredients: [
        { name: 'Chicken thighs', qty: '1.5', unit: 'lb',  estCost: 7.5,  purchaseCost: 8.99 },
        { name: 'Bell peppers',   qty: '2',   unit: 'ea',  estCost: 3.0 },
        { name: 'Broccoli',       qty: '1',   unit: 'head', estCost: 2.5 },
        { name: 'Soy sauce',      qty: '3',   unit: 'tbsp', estCost: 0.4 },
        { name: 'Rice',           qty: '2',   unit: 'cup',  estCost: 0.6 },
        { name: 'Garlic',         qty: '3',   unit: 'clove', estCost: 0.2 },
      ],
      note: 'High heat, keep things moving.'
    },
    {
      id: 'd-r2', name: 'Spaghetti bolognese', mealTags: ['dinner'], portions: 4, leftovers: true,
      ingredients: [
        { name: 'Ground beef',     qty: '1',  unit: 'lb',  estCost: 6.5 },
        { name: 'Pasta',           qty: '1',  unit: 'box', estCost: 1.8 },
        { name: 'Crushed tomatoes', qty: '28', unit: 'oz',  estCost: 2.5 },
        { name: 'Onions',          qty: '1',  unit: 'ea',  estCost: 0.6 },
        { name: 'Garlic',          qty: '4',  unit: 'clove', estCost: 0.3 },
        { name: 'Olive oil',       qty: '2',  unit: 'tbsp', estCost: 0.3 },
      ],
    },
    {
      id: 'd-r3', name: 'Greek salad', mealTags: ['lunch'], portions: 2, leftovers: false,
      ingredients: [
        { name: 'Cucumber',     qty: '1', unit: 'ea', estCost: 1.2 },
        { name: 'Tomatoes',     qty: '3', unit: 'ea', estCost: 2.4 },
        { name: 'Feta cheese',  qty: '4', unit: 'oz', estCost: 3.5 },
        { name: 'Kalamata olives', qty: '0.5', unit: 'cup', estCost: 1.5 },
        { name: 'Red onion',    qty: '0.5', unit: 'ea', estCost: 0.4 },
        { name: 'Olive oil',    qty: '2', unit: 'tbsp', estCost: 0.3 },
      ],
    },
    {
      id: 'd-r4', name: 'Sheet-pan salmon', mealTags: ['dinner'], portions: 2, leftovers: false,
      ingredients: [
        { name: 'Salmon fillets', qty: '2', unit: 'ea', estCost: 11.0 },
        { name: 'Asparagus',      qty: '1', unit: 'bunch', estCost: 3.5 },
        { name: 'Lemon',          qty: '1', unit: 'ea', estCost: 0.8 },
        { name: 'Olive oil',      qty: '2', unit: 'tbsp', estCost: 0.3 },
        { name: 'Garlic',         qty: '2', unit: 'clove', estCost: 0.2 },
      ],
    },
  ];

  const ws = weekStartIso(todayIso);
  const mealPlan = [
    { id: 'd-mp1', date: addDaysIso(ws, 0), mealType: 'dinner', recipeId: 'd-r2' },
    { id: 'd-mp2', date: addDaysIso(ws, 1), mealType: 'dinner', recipeId: 'd-r1' },
    { id: 'd-mp3', date: addDaysIso(ws, 1), mealType: 'lunch',  recipeId: 'd-r3' },
    { id: 'd-mp4', date: addDaysIso(ws, 2), mealType: 'dinner', recipeId: 'd-r4' },
    { id: 'd-mp5', date: addDaysIso(ws, 3), mealType: 'dinner', recipeId: 'd-r2' },
    { id: 'd-mp6', date: addDaysIso(ws, 4), mealType: 'lunch',  recipeId: 'd-r3' },
    { id: 'd-mp7', date: addDaysIso(ws, 5), mealType: 'dinner', recipeId: 'd-r1' },
  ];

  const pantry = [
    { id: 'd-p1',  name: 'Olive oil',     category: 'oils' },
    { id: 'd-p2',  name: 'Salt',          category: 'spices' },
    { id: 'd-p3',  name: 'Black pepper',  category: 'spices' },
    { id: 'd-p4',  name: 'Pasta',         category: 'dry-goods' },
    { id: 'd-p5',  name: 'Rice',          category: 'dry-goods' },
    { id: 'd-p6',  name: 'Garlic',        category: 'produce' },
    { id: 'd-p7',  name: 'Onions',        category: 'produce', lowOnStock: true },
    { id: 'd-p8',  name: 'Eggs',          category: 'fridge' },
    { id: 'd-p9',  name: 'Butter',        category: 'fridge', lowOnStock: true },
    { id: 'd-p10', name: 'Bread',         category: 'bread', lowOnStock: true },
    { id: 'd-p11', name: 'Soy sauce',     category: 'condiments' },
    { id: 'd-p12', name: 'Flour',         category: 'baking' },
  ];

  return {
    income: 7200,
    incomeLabel: 'John & Sarah salaries',
    fixed,
    expenses,
    extra: [],
    tags,
    family,
    goals,
    history: [],
    pool: 2400,
    poolLedger: [],
    closedMonths: [],
    tasks,
    taskGroups,
    recipes,
    mealPlan,
    pantry,
  };
}

Object.assign(window, { buildDemoState });
