// Sample data for the EMPHASIS Tracking System prototype.
// Names/numbers are illustrative.

const TEAM = [
  { id: 'u1', name: 'พิมพ์ชนก ศ.', role: 'BT', avatar: 'พ', color: '#2F5DE5', ppv: 7400, newPeople: 3, hm: 2, badge: 'Top Performer' },
  { id: 'u2', name: 'ธีรวุฒิ จ.', role: 'BT', avatar: 'ธ', color: '#8B5CF6', ppv: 6100, newPeople: 2, hm: 1 },
  { id: 'u3', name: 'อรณิชา ก.', role: 'BT', avatar: 'อ', color: '#10B981', ppv: 5200, newPeople: 4, hm: 3, badge: 'Recruiter' },
  { id: 'u4', name: 'ณัฐพล ว.', role: 'ABO', avatar: 'ณ', color: '#F59E0B', ppv: 3800, newPeople: 1, hm: 1 },
  { id: 'u5', name: 'สุชญา ภ.', role: 'ABO', avatar: 'ส', color: '#EF4444', ppv: 3200, newPeople: 2, hm: 0 },
  { id: 'u6', name: 'กิตติพงษ์ ร.', role: 'Member', avatar: 'ก', color: '#06B6D4', ppv: 1100, newPeople: 0, hm: 0 },
  { id: 'u7', name: 'ปวีณ์ธิดา น.', role: 'ABO', avatar: 'ป', color: '#EC4899', ppv: 2700, newPeople: 1, hm: 1 },
  { id: 'u8', name: 'รวิภาส ท.', role: 'Member', avatar: 'ร', color: '#84CC16', ppv: 500, newPeople: 0, hm: 0 },
];

const LEAD_SOURCES = [
  { id: 'fb',    label: 'Facebook',   color: '#1877F2' },
  { id: 'ig',    label: 'Instagram',  color: '#E1306C' },
  { id: 'tt',    label: 'TikTok',     color: '#000000' },
  { id: 'line',  label: 'LINE OA',    color: '#06C755' },
  { id: 'ref',   label: 'คนรู้จัก',     color: '#F59E0B' },
  { id: 'cold',  label: 'Cold List',  color: '#64748B' },
];

const APPT_TYPES = [
  { id: 'BM', label: 'BM',  full: 'Business Model',          desc: 'Step 1 · คุยแผน + Boarding Pack',         color: '#2F5DE5', tint: '#EEF4FF', dur: 90, step: 1 },
  { id: 'BI', label: 'BI',  full: 'Income Plan',             desc: 'Step 2 · Income Plan + Em Begin/Perfect', color: '#8B5CF6', tint: '#F3EFFE', dur: 60, step: 2 },
  { id: 'S3', label: 'S3',  full: '2Y Retire · Register',    desc: 'Step 3 · List + Online Meeting',          color: '#0EA5E9', tint: '#E0F2FE', dur: 60, step: 3 },
  { id: 'S4', label: 'S4',  full: 'ARTISTRY · ESPRING',      desc: 'Step 4 · Product Master + วางเป้า',       color: '#EC4899', tint: '#FCE7F3', dur: 60, step: 4 },
  { id: 'S5', label: 'S5',  full: 'BT to Silver',            desc: 'Step 5 · ฝึกผู้นำ · ปลายทาง',              color: '#F59E0B', tint: '#FEF3C7', dur: 90, step: 5 },
  { id: '6W', label: '6W',  full: 'EM 6W',                   desc: 'ประเมินสุขภาพ / ชั่งน้ำหนัก / 6 สัปดาห์',  color: '#10B981', tint: '#E7F8F1', dur: 45 },
];

const COUNTRIES = [
  { id: 'th', label: 'ไทย',           flag: '🇹🇭', color: '#EF4444' },
  { id: 'au', label: 'ออสเตรเลีย',     flag: '🇦🇺', color: '#1E40AF' },
  { id: 'nz', label: 'นิวซีแลนด์',     flag: '🇳🇿', color: '#0EA5E9' },
  { id: 'us', label: 'อเมริกา',        flag: '🇺🇸', color: '#7C3AED' },
];

// Boarding Pack 0 — sent after a successful BM → BI
const BOARDING_PACK = {
  label: 'Boarding Pack',
  url: 'https://youtube.com/playlist?list=PLCvOGvpolnHKCD5APsWpqKbNUvrc6orgI',
  desc: 'Pack 0 · ฟังให้จบก่อนนัด BI',
};

// Follow Up Process — 5 steps · maps to appointment types BM → BI → S3 → S4 → S5
const FOLLOW_UP_STEPS = [
  { step: 1, type: 'BM', title: 'Business Model',                   items: ['ส่ง Boarding Pack', 'ฉีดวัคซีน', 'นัดต่อ (BI)'],
    link: { label: 'Boarding Pack · Pack 0',    url: 'https://youtube.com/playlist?list=PLCvOGvpolnHKCD5APsWpqKbNUvrc6orgI' } },
  { step: 2, type: 'BI', title: 'Income Plan',                      items: ['ส่ง link เพิ่มเติม', 'ฉีดวัคซีน', 'Em Begin / Em Perfect'],
    link: { label: 'Income Plan Slides',         url: '#income-plan' } },
  { step: 3, type: 'S3', title: '2Y Retire · 4Y Lifestyle · Register', items: ['ส่ง link เพิ่มเติม, นัดต่อ', 'Clear ให้จัดเวลา', 'เข้า Online Meeting'],
    link: { label: '2Y Retire · 4Y Lifestyle',   url: '#2y4y' } },
  { step: 4, type: 'S4', title: 'ARTISTRY · ESPRING',               items: ['ส่ง link เพิ่มเติม', 'Clear ให้นัดคน', 'วางเป้าหมาย'],
    link: { label: 'ARTISTRY / ESPRING Catalog', url: '#artistry-espring' } },
  { step: 5, type: 'S5', title: 'BT to Silver',                     items: ['Product ที่เหลือ', 'ฝึกอื่นๆ', 'ดูแล Downline'],
    link: null /* end of flow */ },
];

const STATUS = [
  { id: 'pending',   label: 'Pending',   color: '#F59E0B', tint: '#FFF6E0' },
  { id: 'completed', label: 'Completed', color: '#10B981', tint: '#E7F8F1' },
  { id: 'followup',  label: 'Follow-up', color: '#2F5DE5', tint: '#EEF4FF' },
  { id: 'cancelled', label: 'Cancelled', color: '#94A3B8', tint: '#F1F5F9' },
];

// This week's appointments (Mon-Sun, indices 0-6; slot is hour offset from 8:00)
const APPOINTMENTS = [
  { id: 'a1', day: 0, start: 9,    dur: 1.5, type: 'BM', prospect: 'คุณวริศรา ม.',  source: 'fb',   status: 'completed', owner: 'u1', country: 'th', notes: 'รอบ 2 — สนใจชุด BodyKey' },
  { id: 'a2', day: 0, start: 14,   dur: 1,   type: 'BI', prospect: 'คุณกฤษณ์ ส.',   source: 'ig',   status: 'pending',   owner: 'u1', country: 'au' },
  { id: 'a3', day: 1, start: 10.5, dur: 0.75,type: '6W', prospect: 'คุณนวพร ต.',   source: 'ref',  status: 'completed', owner: 'u1', country: 'th', notes: 'น้ำหนักลด 1.2kg' },
  { id: 'a4', day: 1, start: 19,   dur: 1.5, type: 'BM', prospect: 'คุณพิชญา ค.',  source: 'tt',   status: 'pending',   owner: 'u1', country: 'us' },
  { id: 'a5', day: 2, start: 13,   dur: 1,   type: 'BI', prospect: 'คุณภัทรพล ห.', source: 'fb',   status: 'followup',  owner: 'u1', country: 'th' },
  { id: 'a6', day: 3, start: 9.5,  dur: 0.75,type: '6W', prospect: 'คุณจารุวรรณ',  source: 'line', status: 'completed', owner: 'u1', country: 'th' },
  { id: 'a7', day: 3, start: 16,   dur: 1.5, type: 'BM', prospect: 'คุณธนกฤต ป.',  source: 'ref',  status: 'pending',   owner: 'u1', country: 'nz' },
  { id: 'a8', day: 4, start: 11,   dur: 1,   type: 'BI', prospect: 'คุณสรารัตน์',   source: 'ig',   status: 'pending',   owner: 'u1', country: 'th' },
  { id: 'a9', day: 4, start: 18.5, dur: 1.5, type: 'BM', prospect: 'คุณอนุชา ป.',  source: 'cold', status: 'cancelled', owner: 'u1', country: 'au' },
  { id:'a10', day: 5, start: 10,   dur: 0.75,type: '6W', prospect: 'คุณวรรณา ส.',  source: 'ref',  status: 'completed', owner: 'u1', country: 'th', notes: 'ลด 2.1kg สะสม' },
  { id:'a11', day: 5, start: 14,   dur: 1.5, type: 'BM', prospect: 'คุณกานต์ ก.',  source: 'fb',   status: 'pending',   owner: 'u1', country: 'us' },
  { id:'a12', day: 6, start: 11,   dur: 1.5, type: 'BM', prospect: 'คุณรชต พ.',    source: 'tt',   status: 'pending',   owner: 'u1', country: 'th' },
];

const DAYS = ['จ.', 'อ.', 'พ.', 'พฤ.', 'ศ.', 'ส.', 'อา.'];
const DAYS_FULL = ['Mon 11', 'Tue 12', 'Wed 13', 'Thu 14', 'Fri 15', 'Sat 16', 'Sun 17'];

// Weight tracking (EM6W) — sample customer
const WEIGHT_LOG = [
  { week: 0, w: 78.2, goal: 70, note: 'Start' },
  { week: 1, w: 77.1, goal: 70 },
  { week: 2, w: 76.0, goal: 70, note: 'BodyKey Meal Replace 2/วัน' },
  { week: 3, w: 75.4, goal: 70 },
  { week: 4, w: 74.6, goal: 70 },
  { week: 5, w: 73.9, goal: 70, note: 'เริ่มออกกำลังกาย 3 วัน/สัปดาห์' },
  { week: 6, w: 73.1, goal: 70 },
];

// 9 Principles for Success
const PRINCIPLES = [
  { id: 1, label: 'Dream',         done: true },
  { id: 2, label: 'Commitment',    done: true },
  { id: 3, label: 'List',          done: true },
  { id: 4, label: 'Contact / Invite', done: true },
  { id: 5, label: 'Show the Plan', done: false, progress: 0.7 },
  { id: 6, label: 'Follow-up',     done: false, progress: 0.5 },
  { id: 7, label: 'Check Progress',done: false, progress: 0.3 },
  { id: 8, label: 'Counseling',    done: false, progress: 0.4 },
  { id: 9, label: 'Duplicate',     done: false, progress: 0.1 },
];

// BT Qualification — 6 checklist items
const BT_CHECKLIST = [
  { id: 'em3',     label: 'ฟัง 3 EM Packs',                 done: true,  detail: '3/3 packs' },
  { id: 'bridge',  label: 'เข้า EM Bridge / MFinity',        done: true,  detail: 'เข้าแล้ว 2 ครั้ง' },
  { id: 'sop',     label: 'ใช้ SOP (Standing Order Program)',done: true,  detail: 'Active' },
  { id: 'ppv',     label: 'มียอด 5,000 PV',                  done: false, detail: '3,800 / 5,000 PV', progress: 0.76 },
  { id: 'member',  label: 'เพิ่ม 1 Member',                  done: true,  detail: '+2 Members' },
  { id: 'abo',     label: 'เพิ่ม 1 ABO',                     done: false, detail: '0 / 1 ABO',         progress: 0 },
];

// Personal Skill Checkup — demo demo demo
const SKILLS = [
  { id: 'demo5',  label: 'สาธิตสินค้า 5 ตัว',  score: 5, max: 5 },
  { id: 'demo20', label: 'สาธิตสินค้า 20 ตัว', score: 12, max: 20 },
  { id: 'slide9', label: 'เล่า 9 Slides',     score: 8, max: 9 },
  { id: 'plan',   label: 'ผ่าแผน',           score: 7, max: 10 },
];

// 5 Steps from BT to Silver
const FIVE_STEPS = [
  { id: 's1', label: 'BT',          ppv: 5000,  done: true,  desc: 'BT Qualified' },
  { id: 's2', label: '15% Pin',     ppv: 15000, done: true,  desc: '1 leg 7,500' },
  { id: 's3', label: '18% Pin',     ppv: 22500, done: false, progress: 0.62, desc: '2 legs 7,500' },
  { id: 's4', label: '21% Pin',     ppv: 30000, done: false, progress: 0.25, desc: '1 leg 15,000' },
  { id: 's5', label: 'Silver',      ppv: 50000, done: false, progress: 0.10, desc: '1 leg @ 21% Bonus' },
];

// Wheel of Life
const WHEEL = [
  { area: 'Career',       score: 8 },
  { area: 'Finance',      score: 6 },
  { area: 'Health',       score: 7 },
  { area: 'Family',       score: 9 },
  { area: 'Relationship', score: 7 },
  { area: 'Growth',       score: 8 },
  { area: 'Fun',          score: 5 },
  { area: 'Spirituality', score: 6 },
];

Object.assign(window, {
  TEAM, LEAD_SOURCES, APPT_TYPES, STATUS, APPOINTMENTS,
  DAYS, DAYS_FULL, WEIGHT_LOG, PRINCIPLES, BT_CHECKLIST,
  SKILLS, FIVE_STEPS, WHEEL,
  COUNTRIES, BOARDING_PACK, FOLLOW_UP_STEPS,
});

// ============== Extended data for restructured modules ==============

// 6W customer roster
const CUSTOMERS_6W = [
  { id: 'c1', name: 'คุณนวพร ต.',     owner: 'u1', start: 78.2, current: 73.1, goal: 70, week: 6, weeks: 6, status: 'active',    streak: 6, lastUpdate: 'วันนี้',     bodykey: ['Meal Replace', 'Tea Mix'], note: 'ลด 5.1 kg, mood ดี' },
  { id: 'c2', name: 'คุณวรรณา ส.',    owner: 'u1', start: 82.4, current: 79.6, goal: 75, week: 3, weeks: 6, status: 'active',    streak: 3, lastUpdate: '2 วัน',     bodykey: ['Meal Replace'],            note: 'ปกติดี' },
  { id: 'c3', name: 'คุณจารุวรรณ',     owner: 'u1', start: 71.0, current: 67.8, goal: 65, week: 4, weeks: 6, status: 'active',    streak: 4, lastUpdate: 'เมื่อวาน',   bodykey: ['Meal Replace', 'Phyto'],   note: 'ออกกำลังต่อเนื่อง' },
  { id: 'c4', name: 'คุณภัทรา ม.',     owner: 'u3', start: 88.0, current: 86.5, goal: 80, week: 2, weeks: 6, status: 'active',    streak: 2, lastUpdate: '3 วัน',     bodykey: ['Meal Replace'],            note: 'กินผิด schedule' },
  { id: 'c5', name: 'คุณกุลธิดา ย.',    owner: 'u2', start: 65.5, current: 62.0, goal: 58, week: 5, weeks: 6, status: 'active',    streak: 5, lastUpdate: 'วันนี้',     bodykey: ['Tea Mix', 'Phyto'],        note: 'รอบเอวลด 4 cm' },
  { id: 'c6', name: 'คุณสมพร ก.',     owner: 'u1', start: 94.0, current: 89.3, goal: 80, week: 6, weeks: 6, status: 'graduated', streak: 6, lastUpdate: '1 สัปดาห์',  bodykey: ['Meal Replace', 'Tea Mix'], note: 'จบคอร์ส รอบใหม่' },
  { id: 'c7', name: 'คุณนิภา ว.',      owner: 'u3', start: 76.0, current: 75.4, goal: 68, week: 1, weeks: 6, status: 'starting',  streak: 1, lastUpdate: 'วันนี้',     bodykey: ['Meal Replace'],            note: 'สัปดาห์แรก' },
  { id: 'c8', name: 'คุณรพีพรรณ',      owner: 'u2', start: 70.0, current: 68.5, goal: 64, week: 0, weeks: 0, status: 'paused',    streak: 0, lastUpdate: '2 สัปดาห์', bodykey: [],                          note: 'หยุดชั่วคราว ไปต่างจังหวัด' },
];

// ABO / Downline tracking — extended details
const ABO_LIST = [
  { id: 'u1', name: 'พิมพ์ชนก ศ.',    role: 'BT',     abo: 'TH-9201853',  upline: 'me',  joinDate: '2024-08-12', active: true,  pin: '18%',  lastLogin: '5 นาที',  ppv: 7400, downlines: 4,  prospects: 14, bm: 8, bi: 4, packs: 12, btScore: 0.75, lifeSkill: 8.2, country: 'th', focus: true,  badge: 'Top Performer', emPack: 12, mfin: 3, hm: 2 },
  { id: 'u2', name: 'ธีรวุฒิ จ.',       role: 'BT',     abo: 'TH-9214902',  upline: 'me',  joinDate: '2024-10-03', active: true,  pin: '15%',  lastLogin: '1 ชม.',   ppv: 6100, downlines: 3,  prospects: 11, bm: 6, bi: 3, packs: 10, btScore: 0.68, lifeSkill: 7.4, country: 'th', focus: true,  emPack: 10, mfin: 2, hm: 1 },
  { id: 'u3', name: 'อรณิชา ก.',       role: 'BT',     abo: 'TH-9230187',  upline: 'me',  joinDate: '2024-11-22', active: true,  pin: '12%',  lastLogin: '3 ชม.',   ppv: 5200, downlines: 5,  prospects: 16, bm: 9, bi: 5, packs: 14, btScore: 0.82, lifeSkill: 7.8, country: 'th', focus: true,  badge: 'Recruiter', emPack: 14, mfin: 4, hm: 3 },
  { id: 'u4', name: 'ณัฐพล ว.',        role: 'ABO',    abo: 'TH-9241455',  upline: 'u1',  joinDate: '2025-01-08', active: true,  pin: '9%',   lastLogin: '1 วัน',   ppv: 3800, downlines: 2,  prospects: 8,  bm: 4, bi: 2, packs: 8,  btScore: 0.45, lifeSkill: 6.6, country: 'th', focus: true,  emPack: 8,  mfin: 1, hm: 1 },
  { id: 'u5', name: 'สุชญา ภ.',         role: 'ABO',    abo: 'TH-9258304',  upline: 'u1',  joinDate: '2025-02-19', active: true,  pin: '9%',   lastLogin: '2 ชม.',   ppv: 3200, downlines: 1,  prospects: 6,  bm: 3, bi: 2, packs: 6,  btScore: 0.38, lifeSkill: 6.2, country: 'au', focus: true,  emPack: 6,  mfin: 1, hm: 0 },
  { id: 'u6', name: 'กิตติพงษ์ ร.',     role: 'Member', abo: 'TH-9267501',  upline: 'u2',  joinDate: '2025-04-04', active: false, pin: 'New',  lastLogin: '12 วัน',  ppv: 1100, downlines: 0,  prospects: 2,  bm: 1, bi: 0, packs: 2,  btScore: 0.10, lifeSkill: 5.0, country: 'th', focus: false, emPack: 2,  mfin: 0, hm: 0 },
  { id: 'u7', name: 'ปวีณ์ธิดา น.',     role: 'ABO',    abo: 'TH-9270922',  upline: 'u3',  joinDate: '2025-05-14', active: true,  pin: '9%',   lastLogin: '6 ชม.',   ppv: 2700, downlines: 1,  prospects: 5,  bm: 3, bi: 1, packs: 5,  btScore: 0.30, lifeSkill: 6.8, country: 'th', focus: false, emPack: 5,  mfin: 1, hm: 1 },
  { id: 'u8', name: 'รวิภาส ท.',        role: 'Member', abo: 'TH-9288134',  upline: 'u2',  joinDate: '2025-06-30', active: false, pin: 'New',  lastLogin: '4 วัน',   ppv: 500,  downlines: 0,  prospects: 1,  bm: 0, bi: 0, packs: 1,  btScore: 0.05, lifeSkill: 5.2, country: 'nz', focus: false, emPack: 1,  mfin: 0, hm: 0 },
  { id: 'u9', name: 'ชญานิน อ.',        role: 'ABO',    abo: 'TH-9291740',  upline: 'u1',  joinDate: '2025-07-10', active: true,  pin: '9%',   lastLogin: '20 นาที', ppv: 2200, downlines: 0,  prospects: 4,  bm: 2, bi: 1, packs: 4,  btScore: 0.22, lifeSkill: 6.4, country: 'th', focus: false, emPack: 4,  mfin: 1, hm: 0 },
  { id:'u10', name: 'มนัสนันท์ ค.',     role: 'Member', abo: 'TH-9302515',  upline: 'u3',  joinDate: '2025-08-22', active: true,  pin: 'New',  lastLogin: '4 ชม.',   ppv: 800,  downlines: 0,  prospects: 3,  bm: 1, bi: 1, packs: 3,  btScore: 0.15, lifeSkill: 5.6, country: 'us', focus: false, emPack: 3,  mfin: 0, hm: 0 },
];

// Top Uplines (the user's upline chain — for accessing their calendars to coordinate)
const UPLINES = [
  { id: 'ul1', name: 'อาจารย์สมชาย ว.', pin: 'Diamond',         abo: 'TH-1024580', color: '#1B379F', avatar: 'ส', online: true,  org: '4,200 PPV' },
  { id: 'ul2', name: 'พี่นภา ก.',         pin: 'Emerald',         abo: 'TH-2204881', color: '#10B981', avatar: 'น', online: true,  org: '1,800 PPV' },
  { id: 'ul3', name: 'พี่ปริญ พ.',         pin: 'Platinum',        abo: 'TH-3404912', color: '#7C3AED', avatar: 'ป', online: false, org: '900 PPV' },
];

// Social platforms — engagement summary
const SOCIAL_PLATFORMS = [
  { id: 'ig',     label: 'Instagram', color: '#E1306C', icon: 'ig',    followers: 4820, growth: 5.2, posts: 47, eng: 6.1, reach: 18200, connected: true,  user: '@pim.business' },
  { id: 'tt',     label: 'TikTok',    color: '#000000', icon: 'tt',    followers: 12400,growth: 12.8,posts: 22, eng: 8.4, reach: 84600, connected: true,  user: '@pim_emphasis' },
  { id: 'fb',     label: 'Facebook',  color: '#1877F2', icon: 'fb',    followers: 2810, growth: 1.4, posts: 31, eng: 3.2, reach: 6200,  connected: true,  user: 'Pim Business' },
  { id: 'th',     label: 'Threads',   color: '#000000', icon: 'th',    followers: 1240, growth: 22.4,posts: 18, eng: 4.8, reach: 3400,  connected: true,  user: '@pim.business' },
  { id: 'l8',     label: 'Lemon8',    color: '#FFB400', icon: 'l8',    followers: 680,  growth: 8.1, posts: 12, eng: 5.6, reach: 1800,  connected: false, user: '—' },
  { id: 'yt',     label: 'YouTube',   color: '#FF0000', icon: 'yt',    followers: 320,  growth: 3.0, posts: 8,  eng: 9.2, reach: 4400,  connected: true,  user: 'Pim Emphasis' },
];

// Mass-trending content topics (what's getting reach right now)
const TRENDING_TOPICS = [
  { id: 't1', topic: 'การพัฒนาตัวเอง',         emoji: '🌱', volume: 'High',  trend: 18, hashtags: ['#พัฒนาตัวเอง', '#mindset', '#growth'],                  hot: true  },
  { id: 't2', topic: 'การทำธุรกิจ',             emoji: '💼', volume: 'High',  trend: 12, hashtags: ['#ธุรกิจ', '#entrepreneur', '#sidebusiness'],            hot: true  },
  { id: 't3', topic: 'เศรษฐกิจ / การเงิน',      emoji: '📈', volume: 'Rising',trend: 24, hashtags: ['#เศรษฐกิจ', '#การเงิน', '#รายได้เสริม'],               hot: true  },
  { id: 't4', topic: 'Lifestyle / Wellness',    emoji: '✨', volume: 'High',  trend: 8,  hashtags: ['#lifestyle', '#wellness', '#healthylife'],                hot: false },
  { id: 't5', topic: 'ลดน้ำหนัก / สุขภาพ',     emoji: '🏃', volume: 'High',  trend: 14, hashtags: ['#ลดน้ำหนัก', '#bodykey', '#healthyfood'],               hot: false },
  { id: 't6', topic: 'Money Mindset',           emoji: '🧠', volume: 'Rising',trend: 31, hashtags: ['#moneymindset', '#wealth', '#financialfreedom'],         hot: true  },
];

// Today's 5 content ideas (the "5 ต่อวัน" planning system)
const CONTENT_TODAY = [
  { id: 'p1', slot: 1, hook: 'มอร์นิ่ง วิว ออกกำลังกาย', topic: 't4', format: 'Reel · 15s',  platforms: ['ig','tt','th'],    status: 'posted',    time: '06:30', perf: { v: 4200, e: 280 } },
  { id: 'p2', slot: 2, hook: '3 บทเรียนจากการทำธุรกิจสัปดาห์นี้', topic: 't2', format: 'Carousel · 5 slides', platforms: ['ig','fb'],   status: 'scheduled', time: '12:00', perf: null },
  { id: 'p3', slot: 3, hook: 'รีวิว BodyKey หลังใช้ 6W', topic: 't5', format: 'Reel · 30s',   platforms: ['ig','tt','l8'],    status: 'draft',     time: '15:00', perf: null },
  { id: 'p4', slot: 4, hook: 'Money mindset ที่เปลี่ยนชีวิตฉัน', topic: 't6', format: 'Thread · 5 posts', platforms: ['th','fb'],     status: 'idea',      time: '18:30', perf: null },
  { id: 'p5', slot: 5, hook: 'Night routine ของคนทำธุรกิจ', topic: 't4', format: 'Reel · 30s',   platforms: ['ig','tt'],         status: 'idea',      time: '21:00', perf: null },
];

// 15 EM Pack list — YouTube
const EM_PACKS = [
  { num: 0,  title: 'Boarding Pack',          dur: '45 min',  url: 'https://youtube.com/playlist?list=PLCvOGvpolnHKCD5APsWpqKbNUvrc6orgI' },
  { num: 1,  title: 'Dream & Vision',         dur: '38 min',  url: '#' },
  { num: 2,  title: 'Commitment',             dur: '42 min',  url: '#' },
  { num: 3,  title: 'List Building',          dur: '50 min',  url: '#' },
  { num: 4,  title: 'Contact & Invite',       dur: '55 min',  url: '#' },
  { num: 5,  title: 'Show the Plan',          dur: '90 min',  url: '#' },
  { num: 6,  title: 'Follow-up',              dur: '40 min',  url: '#' },
  { num: 7,  title: 'Check Progress',         dur: '35 min',  url: '#' },
  { num: 8,  title: 'Counseling',             dur: '60 min',  url: '#' },
  { num: 9,  title: 'Duplicate',              dur: '45 min',  url: '#' },
  { num: 10, title: 'BT Mindset',             dur: '50 min',  url: '#' },
  { num: 11, title: '5 Steps to Silver',      dur: '70 min',  url: '#' },
  { num: 12, title: 'Product Mastery',        dur: '65 min',  url: '#' },
  { num: 13, title: 'House Meeting Mastery',  dur: '55 min',  url: '#' },
  { num: 14, title: 'Leadership',             dur: '75 min',  url: '#' },
];

// Slide library
const SLIDES = [
  { id: 's1',  group: '9 Slides',      title: 'Slide 1 · Why Amway',                pages: 9,  thumb: '📊', updated: '2 days' },
  { id: 's2',  group: '9 Slides',      title: 'Slide 2 · Business Model',           pages: 9,  thumb: '💼', updated: '2 days' },
  { id: 's3',  group: '9 Slides',      title: 'Slide 3 · Income Plan',              pages: 9,  thumb: '💰', updated: '1 week' },
  { id: 's4',  group: 'Product',       title: 'Nutrilite Master Deck',              pages: 22, thumb: '🌿', updated: '3 weeks' },
  { id: 's5',  group: 'Product',       title: 'BodyKey Catalog',                    pages: 16, thumb: '🥤', updated: '1 month' },
  { id: 's6',  group: 'Product',       title: 'Artistry Skin Care',                 pages: 24, thumb: '✨', updated: '2 weeks' },
  { id: 's7',  group: 'Plan',          title: 'ผ่าแผน · Complete',                  pages: 38, thumb: '🗺️', updated: '1 week' },
  { id: 's8',  group: 'Plan',          title: '5 Steps to Silver',                  pages: 14, thumb: '👑', updated: '4 days' },
  { id: 's9',  group: 'Training',      title: 'House Meeting Playbook',             pages: 12, thumb: '🏠', updated: '6 days' },
];

// Admin: scheduled meetings / broadcasts
const ADMIN_MEETINGS = [
  { id: 'm1', day: 'จันทร์',    time: '20:00-22:00', title: 'Weekly Meeting',          type: 'meeting', recurring: true,  channels: ['line','calendar'], reach: 14 },
  { id: 'm2', day: 'พฤหัสบดี', time: '20:00-21:30', title: 'EM Bridge',               type: 'meeting', recurring: true,  channels: ['line','calendar'], reach: 14 },
  { id: 'm3', day: 'เสาร์',     time: '14:00-17:00', title: 'House Meeting · พี่ภา',   type: 'meeting', recurring: false, channels: ['line','calendar'], reach: 8  },
  { id: 'm4', day: 'อาทิตย์',   time: '10:00-12:00', title: 'New ABO Orientation',     type: 'meeting', recurring: true,  channels: ['line'],            reach: 6  },
];

const ADMIN_BROADCASTS = [
  { id: 'b1', sentAt: '2 ชม.', subject: 'Reminder: Weekly Meeting คืนนี้ 20:00', channels: ['line'],            audience: 'all DLs',      sent: 14, opened: 12 },
  { id: 'b2', sentAt: '1 วัน', subject: 'New EM Pack 14 มาแล้ว — Leadership',     channels: ['line','calendar'], audience: 'BT + ABO',    sent: 10, opened: 9  },
  { id: 'b3', sentAt: '3 วัน', subject: 'ประกาศ: House Meeting ที่บ้านพี่ภา เสาร์ 14:00', channels: ['line','calendar'], audience: 'Selected 8', sent: 8, opened: 8 },
];

Object.assign(window, {
  CUSTOMERS_6W, ABO_LIST, UPLINES,
  SOCIAL_PLATFORMS, TRENDING_TOPICS, CONTENT_TODAY,
  EM_PACKS, SLIDES,
  ADMIN_MEETINGS, ADMIN_BROADCASTS,
});

// ============================================================================
// Supabase sync — hydrate the mocks above with real data when SB is configured.
// ============================================================================
// All views read from window.* (e.g. window.APPT_TYPES, window.APPOINTMENTS, ...)
// so we only need to overwrite those globals once the network round-trip lands.
// If `window.SB_READY` is false, the mocks above stay in place (offline / dev).
//
// Views that need a "loaded once" signal can read `window.DATA_READY` (a Promise).
// ============================================================================
window.DATA_MODE = 'mock';     // overwritten when live data lands
window.DATA_READY = (async () => {
  if (!window.SB_READY || !window.db) {
    console.log('[Data] Running with MOCK data (Supabase not connected).');
    window.DATA_MODE = 'mock';
    return { mode: 'mock' };
  }
  try {
    const results = await Promise.allSettled([
      window.db.apptTypes.list(),
      window.db.leadSources.list(),
      window.db.appointments.list(),
      window.db.customers.list(),
      window.db.meetings.list(),
      window.db.broadcasts.list(),
      window.db.users.list(),
    ]);
    const [types, sources, appts, customers, meetings, broadcasts, downlines] =
      results.map(r => r.status === 'fulfilled' ? r.value : { data: null, error: r.reason });

    let liveTablesLoaded = 0;
    if (types?.data?.length) {
      window.APPT_TYPES = types.data.map(t => ({
        id: t.id, label: t.label, full: t.full_name, desc: t.description,
        color: t.color, tint: t.tint, dur: t.default_duration, step: t.step,
      }));
      liveTablesLoaded++;
    }
    if (sources?.data?.length)    { window.LEAD_SOURCES     = sources.data;    liveTablesLoaded++; }
    if (appts?.data?.length)      { window.APPOINTMENTS     = appts.data;      liveTablesLoaded++; }
    if (customers?.data?.length)  { window.CUSTOMERS_6W     = customers.data;  liveTablesLoaded++; }
    if (meetings?.data?.length)   { window.ADMIN_MEETINGS   = meetings.data;   liveTablesLoaded++; }
    if (broadcasts?.data?.length) { window.ADMIN_BROADCASTS = broadcasts.data; liveTablesLoaded++; }
    if (downlines?.data?.length)  { window.ABO_LIST         = downlines.data;  liveTablesLoaded++; }

    // First error (for diagnostics)
    const firstErr = results.find(r => r.status === 'fulfilled' && r.value?.error)?.value.error;
    if (firstErr) console.warn('[Data] Some Supabase queries returned errors:', firstErr);

    if (liveTablesLoaded > 0) {
      window.DATA_MODE = 'live';
      console.log(`[Data] Loaded ${liveTablesLoaded} table(s) from Supabase ✓`);
    } else {
      window.DATA_MODE = 'empty';
      console.log('[Data] Supabase connected but all queries returned 0 rows — using mocks. Did you run schema.sql + seed-dev.sql?');
    }
    // Notify any mounted components to re-render
    window.dispatchEvent(new CustomEvent('emphasis:data-ready', { detail: { mode: window.DATA_MODE } }));
    return { mode: window.DATA_MODE };
  } catch (e) {
    console.error('[Data] Supabase load failed — staying on mocks:', e);
    window.DATA_MODE = 'mock-fallback';
    return { mode: 'mock-fallback', error: e };
  }
})();
