// Mock data store + helpers — global window-attached
const todayMetrics = {
  sleepScore: 87,
  sleepScoreDelta: 12,
  totalSleep: { h: 7, m: 47 },
  efficiency: 91,
  restingHR: 52,
  hrv: 68,
  respiratory: 13.6,
  readiness: 82,
  recovery: 76,
  activity: 80,
  stress: 42,
  mood: 78,
  steps: 8234,
  stepsGoal: 10000,
  caloriesActive: 412,
  caloriesGoal: 600,
  workouts: 1,
  waterMl: 1450,
  waterGoalMl: 2200,
};

const sleepStages = [
  // 23:45 -> 07:32, simplified buckets
  { t: '23:45', awake: 0.1, rem: 0.0, light: 0.6, deep: 0.3 },
  { t: '00:15', awake: 0.0, rem: 0.0, light: 0.4, deep: 0.6 },
  { t: '00:45', awake: 0.0, rem: 0.0, light: 0.3, deep: 0.7 },
  { t: '01:15', awake: 0.0, rem: 0.2, light: 0.5, deep: 0.3 },
  { t: '01:45', awake: 0.0, rem: 0.4, light: 0.5, deep: 0.1 },
  { t: '02:15', awake: 0.0, rem: 0.0, light: 0.4, deep: 0.6 },
  { t: '02:45', awake: 0.05, rem: 0.0, light: 0.45, deep: 0.5 },
  { t: '03:15', awake: 0.0, rem: 0.5, light: 0.4, deep: 0.1 },
  { t: '03:45', awake: 0.0, rem: 0.6, light: 0.4, deep: 0.0 },
  { t: '04:15', awake: 0.0, rem: 0.1, light: 0.6, deep: 0.3 },
  { t: '04:45', awake: 0.1, rem: 0.0, light: 0.7, deep: 0.2 },
  { t: '05:15', awake: 0.0, rem: 0.5, light: 0.5, deep: 0.0 },
  { t: '05:45', awake: 0.0, rem: 0.7, light: 0.3, deep: 0.0 },
  { t: '06:15', awake: 0.05, rem: 0.3, light: 0.65, deep: 0.0 },
  { t: '06:45', awake: 0.1, rem: 0.4, light: 0.5, deep: 0.0 },
  { t: '07:15', awake: 0.15, rem: 0.2, light: 0.65, deep: 0.0 },
];

const last7DaysSleep = [
  { day: 'Mon', score: 78, hours: 7.1 },
  { day: 'Tue', score: 81, hours: 7.5 },
  { day: 'Wed', score: 72, hours: 6.5 },
  { day: 'Thu', score: 84, hours: 7.8 },
  { day: 'Fri', score: 69, hours: 6.0 },
  { day: 'Sat', score: 89, hours: 8.2 },
  { day: 'Sun', score: 87, hours: 7.8 },
];

const recommendations = [
  { id: 'r1', icon: '🌙', tone: 'violet', title: 'Wind Down Meditation', sub: '10 min · before bed', tag: 'Sleep' },
  { id: 'r2', icon: '💊', tone: 'mint', title: 'Magnesium Glycinate', sub: '300mg · with dinner', tag: 'Supplement' },
  { id: 'r3', icon: '☀️', tone: 'amber', title: 'Morning Light Exposure', sub: 'Get 10 min sunlight', tag: 'Routine' },
  { id: 'r4', icon: '🍵', tone: 'cyan', title: 'Cut Caffeine After 2pm', sub: 'Sensitivity high today', tag: 'Diet' },
];

/* Sleep Coach screen — mobile MVP-style data */
const sleepPersona = {
  tag: 'Low Battery Warrior',
  title: 'Today is about steady energy.',
  body:
    "Last night was 7h 47m with strong deep sleep, but HRV is 68ms — slightly below your baseline. Front-load light, eat warm carbs early, skip the hard workout.",
  trend: 'up', // up | down
  trendDelta: 12, // points vs avg
};

const sleepDailyRecs = [
  {
    id: 'sr1',
    cat: 'BREAKFAST',
    icon: '🥣',
    title: 'Warm protein breakfast',
    rationale: 'Stable glucose on a short-sleep day.',
    kind: 'food',
  },
  {
    id: 'sr2',
    cat: 'ACTIVITY',
    icon: '🚶',
    title: 'Zone 2 walk, not HIIT',
    rationale: 'Light movement beats hard training today.',
    kind: 'activity',
  },
  {
    id: 'sr3',
    cat: 'HYDRATION',
    icon: '💧',
    title: '500ml water before lunch',
    rationale: 'Front-loading water fixes headaches early.',
    kind: 'lifestyle',
  },
  {
    id: 'sr4',
    cat: 'WIND-DOWN',
    icon: '🌙',
    title: 'Lights down 9:30pm',
    rationale: 'Earlier dim light means deeper early-night sleep.',
    kind: 'mind',
  },
];

/* 7-day ribbon — last 7 nights with today highlighted */
const sleepWeekRibbon = [
  { day: 'Mon', score: 72, color: '#fcd34d' },
  { day: 'Tue', score: 81, color: '#8fe3b8' },
  { day: 'Wed', score: 65, color: '#ff8a7a' },
  { day: 'Thu', score: 84, color: '#8fe3b8' },
  { day: 'Fri', score: 69, color: '#fcd34d' },
  { day: 'Sat', score: 89, color: '#8fe3b8' },
  { day: 'Sun', score: 87, color: '#b8a9ff', today: true },
];

/* Night biometrics — evidence behind the sleep score */
const sleepBiometrics = [
  { k: 'HRV',         v: '68',   unit: 'ms',   note: '↓ below baseline', tone: 'warn' },
  { k: 'Resting HR',  v: '56',   unit: 'bpm',  note: '↑ +3 vs avg',      tone: 'warn' },
  { k: 'Respiratory', v: '14.2', unit: '/min', note: 'normal',           tone: 'good' },
  { k: 'Blood O₂',    v: '97',   unit: '%',    note: 'normal',           tone: 'good' },
  { k: 'Skin temp',   v: '+0.3', unit: '°C',   note: 'above baseline',   tone: 'warn' },
  { k: 'Efficiency',  v: '92',   unit: '%',    note: 'good',             tone: 'good' },
];

const agents = [
  {
    id: 'sleep',
    name: 'Sleep Coach',
    emoji: '🌙',
    color: '#b8a9ff',
    tagline: 'Analyzes your sleep and gives personalized statistics.',
    suggestions: [
      'Why did I sleep poorly last night?',
      'Analyze my last 7 days',
      "What's my sleep debt?",
      'How can I get more deep sleep?',
    ],
    intro: "I've reviewed last night's sleep. Your bedtime was 1h 15m later than usual, and caffeine intake at 4:30 PM may have impacted onset. Stress levels were elevated in the evening. Try winding down earlier and avoiding caffeine after 2 PM. Shall I create a plan for today?",
  },
  {
    id: 'wellness',
    name: 'Wellness Agent',
    emoji: '✨',
    color: '#7ad8e0',
    tagline: 'Aggregates your data and suggests routines, supplements & check-ins.',
    suggestions: [
      'Suggest supplements for me',
      'Help me wind down tonight',
      "I'm feeling stressed",
      'Build my evening routine',
    ],
    intro: "Based on your HRV trend (68ms — slightly below your baseline) and elevated evening stress, I recommend Magnesium Glycinate 300mg, a 10-minute box-breathing session at 9:30pm, and dimming your lights after sundown. Want me to schedule a check-in tonight?",
  },
  {
    id: 'meal',
    name: 'Meal Planner',
    emoji: '🥗',
    color: '#8fe3b8',
    tagline: 'Turns supplement & nutrition needs into recipes you actually want to eat.',
    suggestions: [
      'Plan tomorrow’s meals',
      'High-magnesium dinner ideas',
      'I have 30 min and chicken',
      'Make me a sleep-supporting smoothie',
    ],
    intro: "I noticed Wellness recommended boosting magnesium and tryptophan. Tonight I'd suggest miso-glazed salmon with quinoa and sautéed spinach — all aligned with your goals. Want me to send the ingredient list to your Shopping Agent?",
  },
  {
    id: 'shopping',
    name: 'Shopping Agent',
    emoji: '🛒',
    color: '#fcd34d',
    tagline: 'Sources ingredients and supplements — pays in SLEEP if you want.',
    suggestions: [
      'Build my cart for this week',
      'Find Magnesium Glycinate',
      'Lowest price on quinoa',
      'Restock my pantry',
    ],
    intro: "I've drafted a cart for tomorrow's recipes. 6 items, est. delivery tomorrow 10am. You can pay with SLEEP for 5% off, or via your linked card. Want to review the cart?",
  },
];

const marketplace = [
  { id: 'm1', cat: 'Sleep Program', title: 'Deep Sleep Reset', desc: '7-day program to improve sleep quality and recovery.', price: 600, rating: 4.8, reviews: 1240 },
  { id: 'm2', cat: 'Nutrition', title: 'Personalised Nutrition', desc: 'AI meal plans based on your goals and biometrics.', price: 800, rating: 4.7, reviews: 932 },
  { id: 'm3', cat: 'Mindfulness', title: 'Mindful Mornings', desc: 'Guided meditations for clarity, focus and calm starts.', price: 400, rating: 4.9, reviews: 2104 },
  { id: 'm4', cat: 'Wearable', title: 'Smart Ring Pro', desc: 'Advanced sleep tracking and recovery insights.', price: 1200, rating: 4.6, reviews: 580, img: (window.__resources && window.__resources.smartRing) || '/sleep/assets/smart-ring.png' },
  { id: 'm5', cat: 'Recovery', title: 'Cold Plunge Protocol', desc: '21-day adaptive cold exposure programme.', price: 550, rating: 4.5, reviews: 412 },
  { id: 'm6', cat: 'Fitness', title: 'Zone 2 Cardio Plan', desc: '8 weeks of personalised low-intensity cardio.', price: 700, rating: 4.7, reviews: 670 },
  { id: 'm7', cat: 'Supplements', title: 'Magnesium Glycinate', desc: '300mg pharmaceutical-grade · 60 capsules.', price: 220, rating: 4.9, reviews: 3290 },
  { id: 'm8', cat: 'Sleep Program', title: 'Snore-less Nights', desc: 'Breathwork + posture programme for quieter sleep.', price: 350, rating: 4.4, reviews: 211 },
];

const rewards = {
  available: 2845,
  staked: 1250,
  unlockDays: 28,
  daily: [
    { id: 'd1', icon: '✅', title: 'Daily Check-in', amount: 10, claimed: true },
    { id: 'd2', icon: '😴', title: 'Sleep Quality', sub: '87 Score', amount: 25, claimed: true },
    { id: 'd3', icon: '🧘', title: 'Meditation Session', sub: '+15 SLEEP', amount: 15, claimed: true },
    { id: 'd4', icon: '👟', title: 'Steps Goal', sub: '8,234 / 10,000 steps', amount: 20, claimed: false },
    { id: 'd5', icon: '💧', title: 'Hydration', sub: '1.45 / 2.2 L', amount: 10, claimed: false },
  ],
  challenges: [
    { id: 'c1', title: '7-Day Sleep Streak', progress: 5, target: 7, reward: 200 },
    { id: 'c2', title: 'Beat Your HRV', progress: 3, target: 5, reward: 150 },
    { id: 'c3', title: 'Wind Down Master', progress: 4, target: 10, reward: 300 },
  ],
  achievements: [
    { id: 'a1', title: 'First Light', desc: 'Track your first night', unlocked: true },
    { id: 'a2', title: 'Deep Diver', desc: '10 nights of 90+ minutes deep sleep', unlocked: true },
    { id: 'a3', title: 'Zen Mode', desc: 'Complete 30 meditations', unlocked: false, progress: 18, target: 30 },
    { id: 'a4', title: 'Iron Lung', desc: 'Hit HRV 80+ for a week', unlocked: false, progress: 2, target: 7 },
  ],
  benefits: [
    { label: 'Subscription Discount', value: '−25%' },
    { label: 'AI Agent Credits', value: '+20%' },
    { label: 'Marketplace Discount', value: '−10%' },
    { label: 'Challenge Rewards', value: '+15%' },
    { label: 'Partner Perks', value: 'Exclusive' },
  ],
  stats: { total: 12450, daysActive: 45, streak: 12 },
  membership: { tier: 'Gold', level: 3, xp: 12450, xpMax: 20000 },
};

const community = {
  feed: [
    { id: 'f1', user: 'Maya K.', avatar: 'MK', mins: 14, text: "Hit a new HRV PR this morning — 84ms 🚀 4 weeks of consistent wind-downs and cutting caffeine after 2pm.", likes: 42, comments: 8, tag: 'Recovery' },
    { id: 'f2', user: 'Jordan L.', avatar: 'JL', mins: 38, text: "Anyone else find that the magnesium glycinate from the marketplace knocks them out instantly? Sleeping like a baby.", likes: 71, comments: 23, tag: 'Supplements' },
    { id: 'f3', user: 'Aisha R.', avatar: 'AR', mins: 92, text: "Day 30 of the Deep Sleep Reset! Sleep score average went from 71 to 84. Highly recommend.", likes: 128, comments: 31, tag: 'Sleep Program' },
    { id: 'f4', user: 'Tom S.', avatar: 'TS', mins: 220, text: "What's everyone using for blue light blocking after 9pm? Currently on f.lux + amber glasses.", likes: 19, comments: 45, tag: 'Discussion' },
  ],
  challenges: [
    { id: 'cc1', title: 'May Sleep Marathon', participants: 4204, days: 12, reward: '5,000 SLEEP pool' },
    { id: 'cc2', title: 'HRV Rising', participants: 1820, days: 21, reward: '2,000 SLEEP pool' },
    { id: 'cc3', title: 'Mindful May', participants: 9120, days: 31, reward: '10,000 SLEEP pool' },
  ],
  leaders: [
    { rank: 1, name: 'Sora N.', avatar: 'SN', score: 96, streak: 88 },
    { rank: 2, name: 'Lena P.', avatar: 'LP', score: 94, streak: 64 },
    { rank: 3, name: 'Dreamer (you)', avatar: 'D', score: 87, streak: 12, you: true },
    { rank: 4, name: 'Marcus V.', avatar: 'MV', score: 85, streak: 41 },
    { rank: 5, name: 'Ines G.', avatar: 'IG', score: 84, streak: 30 },
  ],
};

const nutrition = {
  todayMacros: { protein: 88, proteinGoal: 140, carbs: 180, carbsGoal: 220, fat: 62, fatGoal: 80, calories: 1840, caloriesGoal: 2200 },
  flagged: [
    { name: 'Magnesium', state: 'low', sub: 'Boost via leafy greens, almonds' },
    { name: 'Vitamin D', state: 'low', sub: 'Sunlight + fatty fish' },
    { name: 'Tryptophan', state: 'ok', sub: 'Within range' },
    { name: 'Iron', state: 'ok', sub: 'Within range' },
  ],
  meals: [
    { id: 'br', slot: 'Breakfast', name: 'Greek yogurt parfait', kcal: 420, why: 'High protein, gut-friendly' },
    { id: 'lu', slot: 'Lunch', name: 'Quinoa power bowl', kcal: 580, why: 'Complete protein + magnesium' },
    { id: 'sn', slot: 'Snack', name: 'Almonds + dark chocolate', kcal: 220, why: 'Magnesium + tryptophan' },
    { id: 'di', slot: 'Dinner', name: 'Miso-glazed salmon', kcal: 620, why: 'Omega-3, vitamin D, sleep-supporting' },
  ],
  cart: [
    { id: 'i1', name: 'Wild salmon fillet', qty: '2 × 180g', price: 18.4 },
    { id: 'i2', name: 'Quinoa', qty: '500g', price: 6.5 },
    { id: 'i3', name: 'Baby spinach', qty: '200g', price: 3.8 },
    { id: 'i4', name: 'Almonds (raw)', qty: '300g', price: 9.2 },
    { id: 'i5', name: 'Greek yogurt', qty: '1kg', price: 7.1 },
    { id: 'i6', name: 'Dark chocolate 85%', qty: '100g', price: 4.4 },
  ],
};

const mindBody = {
  todayMood: null,
  moodHistory: [
    { d: 'Mon', mood: 6 }, { d: 'Tue', mood: 7 }, { d: 'Wed', mood: 5 },
    { d: 'Thu', mood: 7 }, { d: 'Fri', mood: 4 }, { d: 'Sat', mood: 8 }, { d: 'Sun', mood: 7 },
  ],
  practices: [
    { id: 'p1', icon: '🧘', name: 'Box Breathing', mins: 5, level: 'Beginner' },
    { id: 'p2', icon: '🌬️', name: '4-7-8 Breath', mins: 4, level: 'Calming' },
    { id: 'p3', icon: '🌙', name: 'Body Scan', mins: 10, level: 'Wind-down' },
    { id: 'p4', icon: '☀️', name: 'Morning Reset', mins: 7, level: 'Energizing' },
    { id: 'p5', icon: '✍️', name: 'Gratitude Journal', mins: 5, level: 'Reflective' },
    { id: 'p6', icon: '🎧', name: 'Sleep Story', mins: 20, level: 'Sleep' },
  ],
  windDownTasks: [
    { id: 'w1', icon: '☕', name: 'No caffeine', when: 'after 2 PM', done: true },
    { id: 'w2', icon: '📵', name: 'Screens off', when: '1h before bed', done: false },
    { id: 'w3', icon: '🛁', name: 'Warm shower', when: '90 min before bed', done: false },
    { id: 'w4', icon: '🧘', name: 'Box breathing', when: '10 min', done: false },
    { id: 'w5', icon: '📖', name: 'Read fiction', when: '15 min', done: false },
  ],
};

const activity = {
  steps: 8234, stepsGoal: 10000,
  zones: { min: 38, z2: 22, z3: 14, z4: 5, z5: 1 }, // minutes
  workouts: [
    { id: 'w1', type: 'Strength', mins: 48, kcal: 320, when: 'Today, 7:10 AM', heart: 132 },
    { id: 'w2', type: 'Zone 2 Run', mins: 35, kcal: 280, when: 'Yesterday, 6:30 AM', heart: 138 },
    { id: 'w3', type: 'Yoga', mins: 30, kcal: 110, when: 'Sun, 8:00 AM', heart: 96 },
  ],
  weekActivity: [
    { d: 'M', mins: 65 }, { d: 'T', mins: 30 }, { d: 'W', mins: 0 },
    { d: 'T', mins: 80 }, { d: 'F', mins: 45 }, { d: 'S', mins: 120 }, { d: 'S', mins: 50 },
  ],
};

window.MOCK = { todayMetrics, sleepStages, last7DaysSleep, recommendations, agents, marketplace, rewards, community, nutrition, mindBody, activity, sleepPersona, sleepDailyRecs, sleepWeekRibbon, sleepBiometrics };
