const { COMPANIES: MG_CO, LEADERBOARD_PEERS: MG_PEERS, STOCK_PHOTOS: MG_PHOTOS } = window.PryzeData;

const MG_STORES = [
  { id: 's1', name: 'St. Cloud Center', city: 'St. Cloud, MN', staff: 34, head: 40, grade: 'B', rank: 3, of: 28 },
  { id: 's2', name: 'Langston Blvd', city: 'Arlington, VA', staff: 30, head: 30, grade: 'A', rank: 1, of: 28 },
  { id: 's3', name: 'Cedar Grove', city: 'Cedar Grove, NJ', staff: 19, head: 31, grade: 'C', rank: 14, of: 28 },
  { id: 's4', name: 'Harbor Point', city: 'Baltimore, MD', staff: 41, head: 58, grade: 'D', rank: 22, of: 28 },
];

const MG_PERIODS = [
  { k: 'day', label: 'Day', word: 'today', prev: 'yesterday' },
  { k: 'week', label: 'Week', word: 'this week', prev: 'last week' },
  { k: 'month', label: 'Month', word: 'this month', prev: 'last month' },
  { k: 'year', label: 'Year', word: 'this year', prev: 'last year' },
];

/* metrics scale with the period so switching feels real */
const MG_SCALE = { day: 1, week: 6.4, month: 27, year: 320 };
const MG_BASE = {
  s1: { golds: 41, users: 22, hours: 74, delta: 12, engaged: 0.71, redeemed: 1840 },
  s2: { golds: 58, users: 25, hours: 88, delta: 24, engaged: 0.86, redeemed: 2410 },
  s3: { golds: 19, users: 11, hours: 41, delta: -8, engaged: 0.52, redeemed: 760 },
  s4: { golds: 27, users: 16, hours: 63, delta: -3, engaged: 0.44, redeemed: 1120 },
};
const mgMetrics = (store, period) => {
  const b = MG_BASE[store] || MG_BASE.s1, m = MG_SCALE[period] || 1;
  const wob = period === 'day' ? 0 : period === 'month' ? 3 : period === 'year' ? -4 : 1;
  return {
    golds: Math.round(b.golds * m),
    users: period === 'day' ? b.users : Math.min((MG_STORES.find(s => s.id === store) || MG_STORES[0]).staff, Math.round(b.users * 1.25)),
    hours: Math.round(b.hours * m),
    delta: b.delta + wob,
    engaged: Math.min(0.98, b.engaged + (period === 'year' ? 0.08 : 0)),
    redeemed: Math.round(b.redeemed * m),
  };
};

const MG_GRADE_SAY = {
  A: 'Best week your store has had all quarter. Keep the floor coverage exactly as it is.',
  B: 'Solid. Two more goals cleared a shift and you are in A range.',
  C: 'Middle of the pack. Engagement is the gap, not effort.',
  D: 'Slipping. Half the team has not banked a goal in three shifts.',
};

const MG_BUBBLES = [
  { cat: 'Revenue', n: 184, c: '#FF5A2E' },
  { cat: 'Productivity', n: 141, c: '#C6FF3D' },
  { cat: 'HR', n: 72, c: '#9C8FFF' },
];

const MG_TREND = [42, 51, 47, 63, 58, 74, 81, 77, 92, 88, 104, 118];

const mgStaff = (storeId) => {
  const seed = MG_STORES.findIndex(s => s.id === storeId) * 7;
  return MG_PEERS.slice(0, 8).map((p, i) => ({
    id: storeId + '-' + p.id,
    name: p.name,
    photo: p.photo,
    role: p.role,
    golds: 34 - i * 3 + ((seed + i * 5) % 6),
    hours: 61 - i * 4 + ((seed + i * 3) % 7),
    pts: p.points - seed * 40,
    streak: p.streak,
    shift: i % 3 === 0 ? 'On shift' : i % 3 === 1 ? 'Off today' : 'Clocks in 4pm',
    risk: i === 5 ? 'quiet' : i === 7 ? 'streak' : i === 3 ? 'cold' : i === 6 ? 'new' : null,
  }));
};

const MG_REASONS = [
  { k: 'above', label: 'Went above and beyond', say: 'going above and beyond', pts: 150, note: 'Covered something nobody asked them to' },
  { k: 'cover', label: 'Covered a shift last minute', say: 'covering a shift last minute', pts: 200, note: 'Same-day save' },
  { k: 'train', label: 'Trained a new hire', say: 'training a new hire', pts: 120, note: 'Onboarding help' },
  { k: 'safety', label: 'Caught a safety issue', say: 'catching a safety issue', pts: 180, note: 'Flagged before it became a problem' },
  { k: 'customer', label: 'Customer called them out by name', say: 'getting called out by name by a customer', pts: 100, note: 'Named in a review or survey' },
  { k: 'clean', label: 'Left the floor spotless', say: 'leaving the floor spotless', pts: 60, note: 'Close-out quality' },
];

const MG_BOOSTS = [
  { k: 'b50', label: '+50 bonus points', sub: 'Small nudge', pts: '+50', tone: 'orange' },
  { k: 'shield', label: 'Streak Shield', sub: 'One missed day forgiven', pts: '1 day', tone: 'violet' },
  { k: 'b150', label: '+150 bonus points', sub: 'Real reward', pts: '+150', tone: 'orange' },
  { k: 'x3', label: '3× multiplier', sub: 'Triples the goal payout', pts: '3×', tone: 'lime' },
  { k: 'x2', label: '2× multiplier', sub: 'Doubles the goal payout', pts: '2×', tone: 'lime' },
];

const MG_WINDOWS = ['This shift', 'Today', '48 hours', 'This week'];

const MG_GOALS = [
  { id: 'm1', title: 'Curbside handoff under 2 min', cat: 'Revenue', pts: 60, live: true, taken: 14, done: 9, when: 'Daily' },
  { id: 'm2', title: 'Restock endcap, aisle 4', cat: 'Productivity', pts: 80, live: true, taken: 6, done: 4, when: 'Today' },
  { id: 'm3', title: 'Reset the fitting room rail', cat: 'Productivity', pts: 45, live: true, taken: 11, done: 11, when: 'Daily' },
  { id: 'm4', title: 'Walk the safety checklist', cat: 'HR', pts: 120, live: false, taken: 3, done: 1, when: 'Weekly' },
  { id: 'm5', title: 'Shadow a new hire for an hour', cat: 'HR', pts: 140, live: true, taken: 2, done: 2, when: 'This week' },
];

const MG_CATS = ['Revenue', 'Productivity', 'HR'];

const MG_REPORT = {
  engagedUsers: 19, of: 34,
  goldsToday: 41, goldsAvg: 36,
  redeemablePts: 3820,
  redeemed: 12,
  coverage: [
    { block: '6a–10a', staff: 5, need: 5 },
    { block: '10a–2p', staff: 8, need: 7 },
    { block: '2p–6p', staff: 4, need: 7 },
    { block: '6p–10p', staff: 2, need: 5 },
  ],
  notes: [
    'Afternoon block is two people short — Golds Crushed always dips there.',
    'Kudos are up 40% since you started awarding points by name.',
  ],
};

const MG_COMPANY = { rank: 3, of: 28, pctile: 89, storesUp: 21, avgGrade: 'B', myDelta: 2, co: 'walmart' };

/* three share tiers, keyed off the grade */
const MG_TIERS = {
  A: { k: 'a', head: ['WE', 'RAN', 'IT'], tape: 'top of the company', line: 'Nobody touched us.', bg: 'radial-gradient(120% 80% at 12% 0%,#5B4FE0 0%,#2A1F52 46%,#14122A 100%)' },
  B: { k: 'b', head: ['WE', 'SHOWED', 'UP'], tape: 'above the line', line: 'Two goals a shift from an A.', bg: 'radial-gradient(120% 80% at 12% 0%,#2F6E4F 0%,#1B3D30 48%,#101E1A 100%)' },
  C: { k: 'c', head: ['WE', 'PUT', 'IN'], tape: 'middle of the pack', line: 'Engagement is the gap, not effort.', bg: 'radial-gradient(120% 80% at 12% 0%,#8A4A22 0%,#40230F 48%,#1C1209 100%)' },
  D: { k: 'c', head: ['WE', 'PUT', 'IN'], tape: 'climbing back', line: 'Every point from here is ground gained.', bg: 'radial-gradient(120% 80% at 12% 0%,#8A4A22 0%,#40230F 48%,#1C1209 100%)' },
};

/* daily nudge reasons — concrete, one icon each */
const MG_NUDGE_KINDS = {
  quiet: { icon: 'moon', label: 'No goals in three shifts' },
  cold: { icon: 'device-mobile-slash', label: "Hasn't opened the app at peak" },
  streak: { icon: 'flame', label: 'Streak breaks tonight' },
  new: { icon: 'user-plus', label: 'Signed up, never played' },
};

const MG_DAILY_STATS = [
  { k: 'engaged', icon: 'users-three', label: 'engaged users' },
  { k: 'golds', icon: 'target', label: 'goals crushed' },
  { k: 'pts', icon: 'coins', label: 'redeemable pts' },
  { k: 'redeemed', icon: 'gift', label: 'rewards redeemed' },
];

window.PryzeManagerData = { MG_STORES, MG_PERIODS, mgMetrics, MG_GRADE_SAY, MG_BUBBLES, MG_TREND, mgStaff, MG_REASONS, MG_BOOSTS, MG_WINDOWS, MG_GOALS, MG_CATS, MG_REPORT, MG_COMPANY, MG_TIERS, MG_NUDGE_KINDS, MG_DAILY_STATS, MG_CO };
