// hq-data.jsx — Muyassir HQ (platform super-admin) data layer.
// Persona: Muyassir's OWN ops/founder team, managing every tour-operator
// customer (agency) on the platform — billing, growth, support, compliance.
// Distinct from the per-agency "Leader console".

const HQ_USER = { name: 'Layla Osman', role: 'Platform operations', initials: 'LO', tone: 'var(--ug-gold)' };

// ── Platform KPIs ────────────────────────────────────────────────────────
const HQ_KPI = {
  mrr: 11240,            // £/mo recurring
  mrrDeltaPct: 8.2,      // MoM
  activeAgencies: 47,
  totalAgencies: 53,     // incl. trials + churned
  trials: 6,
  pilgrimsLifetime: 2840,
  pilgrimsActive: 1290,  // across upcoming/live batches
  batchesUpcoming: 38,
  batchesLive: 5,
  nrr: 112,              // net revenue retention %
  trialConv: 38,         // trial→paid %
  atRisk: 3,
  newAgenciesMo: 5,
  churnedMo: 1,
};

// 12-month MRR (£k) and pilgrim-onboarding series.
const HQ_MRR_SERIES = [4.1, 4.6, 5.2, 5.9, 6.4, 7.1, 7.8, 8.5, 9.3, 10.0, 10.5, 11.24];
const HQ_PILGRIM_SERIES = [120, 180, 260, 360, 470, 610, 780, 980, 1240, 1560, 1980, 2840];
const HQ_MONTHS = ['Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];

// ── Plans ────────────────────────────────────────────────────────────────
const HQ_PLANS = {
  base:    { label: 'Base',    price: 149, tone: 'var(--ug-ink-soft)', blurb: '1 leader · up to 80 pilgrims per batch' },
  premium: { label: 'Premium', price: 399, tone: 'var(--ug-primary)',  blurb: 'Unlimited leaders & pilgrims · priority support' },
  trial:   { label: 'Trial',   price: 0,   tone: '#5B7A9A',            blurb: '14-day free trial · no card required' },
};
const HQ_PLAN_MIX = { base: 5, premium: 4 };

// ── Account status / health ──────────────────────────────────────────────
// Stripe subscription-status vocabulary (read-only mirror).
const HQ_STATUS = {
  active:   { label: 'Active',   tone: 'var(--ug-success)' },
  trial:    { label: 'Trialing', tone: '#5B7A9A' },
  past_due: { label: 'Past due', tone: '#C77D3A' },
  churned:  { label: 'Canceled', tone: 'var(--ug-muted)' },
};

// Where the live data is mirrored FROM, and what each source is for.
const HQ_SYNC = {
  stripe:   { label: 'Stripe',   color: '#635BFF', synced: '2 min ago', purpose: 'Billing & subscriptions', what: 'Plans, MRR, invoices, payment methods and dunning live in Stripe.' },
  supabase: { label: 'Supabase', color: '#3ECF8E', synced: 'live',      purpose: 'Operator & pilgrim data',  what: 'Accounts, pilgrims, batches, usage and licences live in the Supabase app DB.' },
};
const HQ_HEALTH = {
  good:    { label: 'Healthy',  tone: 'var(--ug-success)' },
  watch:   { label: 'Watch',    tone: 'var(--ug-gold)' },
  at_risk: { label: 'At risk',  tone: 'var(--ug-danger)' },
};

// ── Agencies (the B2B customers) ─────────────────────────────────────────
// mrr 0 = trial/churned. usage = % of seats/feature adoption this month.
const HQ_AGENCIES = [
  { id: 'baytullah', name: 'Baytullah Travel',   country: 'United Kingdom', flag: '🇬🇧', initials: 'BT', tone: '#3F6E59',
    plan: 'premium', status: 'active', health: 'good', mrr: 399, pilgrims: 310, batches: 11, leaders: 8,
    joined: 'Sep 2024', verified: true, owner: 'Mufti Adam Patel', ownerEmail: 'adam@baytullah.co.uk', lastActive: '40 min ago', usage: 0.94, ltv: 4788 },
  { id: 'alnoor',    name: 'Al-Noor Tours',       country: 'Norway',         flag: '🇳🇴', initials: 'AN', tone: 'var(--ug-primary)',
    plan: 'premium', status: 'active', health: 'good', mrr: 399, pilgrims: 86, batches: 4, leaders: 5,
    joined: 'Jan 2025', verified: true, owner: 'Sheikh Yusuf Adeyemi', ownerEmail: 'yusuf@alnoor.tours', lastActive: '2 hours ago', usage: 0.88, ltv: 2394 },
  { id: 'rahma',     name: 'Rahma Pilgrimage',    country: 'United States',  flag: '🇺🇸', initials: 'RP', tone: '#7A4F4F',
    plan: 'premium', status: 'active', health: 'good', mrr: 399, pilgrims: 120, batches: 5, leaders: 6,
    joined: 'Nov 2024', verified: true, owner: 'Imam Khalid Rashid', ownerEmail: 'khalid@rahma.us', lastActive: 'Today', usage: 0.81, ltv: 3192 },
  { id: 'anwar',     name: 'Anwar Hajj & Umrah',  country: 'Nigeria',        flag: '🇳🇬', initials: 'AH', tone: '#6E5B3C',
    plan: 'premium', status: 'active', health: 'good', mrr: 399, pilgrims: 95, batches: 4, leaders: 4,
    joined: 'Oct 2024', verified: true, owner: 'Alhaji Musa Bello', ownerEmail: 'musa@anwar.ng', lastActive: 'Yesterday', usage: 0.76, ltv: 3591 },
  { id: 'firdaus',   name: 'Firdaus Travel',      country: 'Malaysia',       flag: '🇲🇾', initials: 'FT', tone: '#3F6E6E',
    plan: 'base', status: 'active', health: 'watch', mrr: 149, pilgrims: 51, batches: 3, leaders: 3,
    joined: 'Feb 2025', verified: true, owner: 'Nurul Hidayah', ownerEmail: 'nurul@firdaus.my', lastActive: '6 days ago', usage: 0.34, ltv: 745 },
  { id: 'safar',     name: 'Safar Noor',          country: 'UAE',            flag: '🇦🇪', initials: 'SN', tone: '#8A6A2E',
    plan: 'base', status: 'active', health: 'watch', mrr: 149, pilgrims: 42, batches: 2, leaders: 2,
    joined: 'Mar 2025', verified: true, owner: 'Salem Al-Mansoori', ownerEmail: 'salem@safarnoor.ae', lastActive: '3 days ago', usage: 0.52, ltv: 596 },
  { id: 'barakah',   name: 'Barakah Tours',       country: 'Sweden',         flag: '🇸🇪', initials: 'BK', tone: '#5B6E4A',
    plan: 'base', status: 'active', health: 'good', mrr: 149, pilgrims: 44, batches: 2, leaders: 3,
    joined: 'Jan 2025', verified: true, owner: 'Hamza Ahmed', ownerEmail: 'hamza@barakah.se', lastActive: 'Today', usage: 0.79, ltv: 894 },
  { id: 'tayyibah',  name: 'Tayyibah Tours',      country: 'France',         flag: '🇫🇷', initials: 'TT', tone: '#5B5B82',
    plan: 'base', status: 'past_due', health: 'at_risk', mrr: 149, pilgrims: 38, batches: 2, leaders: 2,
    joined: 'Dec 2024', verified: true, owner: 'Yacine Brahimi', ownerEmail: 'yacine@tayyibah.fr', lastActive: '5 days ago', usage: 0.61, ltv: 894, overdueDays: 14 },
  { id: 'madinah',   name: 'Madinah Express',     country: 'Pakistan',       flag: '🇵🇰', initials: 'ME', tone: '#4A5B6E',
    plan: 'base', status: 'active', health: 'good', mrr: 149, pilgrims: 24, batches: 1, leaders: 1,
    joined: 'Apr 2025', verified: true, owner: 'Tariq Sheikh', ownerEmail: 'tariq@madinahexpress.pk', lastActive: 'Today', usage: 0.68, ltv: 298 },
  { id: 'sakina',    name: 'Sakina Travels',      country: 'Canada',         flag: '🇨🇦', initials: 'ST', tone: '#6E4F7A',
    plan: 'trial', status: 'trial', health: 'good', mrr: 0, pilgrims: 18, batches: 1, leaders: 1,
    joined: 'Trial · 6 days left', verified: false, owner: 'Aisha Noor', ownerEmail: 'aisha@sakina.ca', lastActive: '1 hour ago', usage: 0.45, ltv: 0, trialDays: 6 },
  { id: 'ihsan',     name: 'Ihsan Journeys',      country: 'Australia',      flag: '🇦🇺', initials: 'IJ', tone: '#9A6A3C',
    plan: 'trial', status: 'trial', health: 'watch', mrr: 0, pilgrims: 12, batches: 1, leaders: 1,
    joined: 'Trial · 11 days left', verified: false, owner: 'Bilal Karim', ownerEmail: 'bilal@ihsan.au', lastActive: '4 days ago', usage: 0.22, ltv: 0, trialDays: 11 },
  { id: 'noorharam', name: 'Noor ul-Haram',       country: 'Germany',        flag: '🇩🇪', initials: 'NH', tone: 'var(--ug-muted)',
    plan: 'base', status: 'churned', health: 'at_risk', mrr: 0, pilgrims: 0, batches: 0, leaders: 0,
    joined: 'Jun 2024 – May 2026', verified: true, owner: 'Omar Saleh', ownerEmail: 'omar@noorharam.de', lastActive: '32 days ago', usage: 0, ltv: 588, churnedReason: 'Switched to in-house tool' },
];

// ── Activity feed (platform-wide events) ─────────────────────────────────
const HQ_ACTIVITY = [
  { id: 1, type: 'upgrade', agency: 'Baytullah Travel', text: 'upgraded Scale → Enterprise', meta: '+£501 MRR', when: '2h ago' },
  { id: 2, type: 'trial',   agency: 'Sakina Travels',   text: 'started a Growth trial', meta: 'Canada · 18 pilgrims imported', when: '6h ago' },
  { id: 3, type: 'payment', agency: 'Rahma Pilgrimage', text: 'paid invoice INV-2041', meta: '£399', when: 'Today' },
  { id: 4, type: 'risk',    agency: 'Tayyibah Tours',   text: 'invoice overdue 14 days', meta: 'card declined ×2', when: 'Today' },
  { id: 5, type: 'launch',  agency: 'Anwar Hajj & Umrah', text: 'launched batch “Ramadan ’26”', meta: '95 pilgrims', when: 'Yesterday' },
  { id: 6, type: 'churn',   agency: 'Noor ul-Haram',    text: 'cancelled subscription', meta: 'Starter · 11mo tenure', when: '4d ago' },
];

// ── Support tickets ──────────────────────────────────────────────────────
const HQ_TICKETS = [
  { id: 'T-318', agency: 'Al-Noor Tours',    subject: 'Visa status column not syncing from ministry feed', priority: 'high',   status: 'open',     age: '1h',  assignee: 'Layla O.' },
  { id: 'T-317', agency: 'Sakina Travels',   subject: 'How do I bulk-import pilgrims from a spreadsheet?',   priority: 'normal', status: 'open',     age: '3h',  assignee: 'Unassigned' },
  { id: 'T-316', agency: 'Firdaus Travel',   subject: 'Refund request for a cancelled batch',               priority: 'high',   status: 'pending',  age: '1d',  assignee: 'Sami R.' },
  { id: 'T-315', agency: 'Barakah Tours',    subject: 'Add a second leader seat to our plan',               priority: 'low',    status: 'open',     age: '1d',  assignee: 'Layla O.' },
  { id: 'T-314', agency: 'Rahma Pilgrimage', subject: 'Export the ministry manifest as PDF',                priority: 'normal', status: 'resolved', age: '2d',  assignee: 'Sami R.' },
  { id: 'T-313', agency: 'Tayyibah Tours',   subject: 'Update the card on file — payment failing',          priority: 'high',   status: 'pending',  age: '2d',  assignee: 'Layla O.' },
];
const HQ_PRIORITY = {
  high:   { label: 'High',   tone: 'var(--ug-danger)' },
  normal: { label: 'Normal', tone: '#5B7A9A' },
  low:    { label: 'Low',    tone: 'var(--ug-muted)' },
};
const HQ_TICKET_STATUS = {
  open:     { label: 'Open',     tone: 'var(--ug-gold)' },
  pending:  { label: 'Pending',  tone: '#5B7A9A' },
  resolved: { label: 'Resolved', tone: 'var(--ug-success)' },
};

// ── Compliance / verification queue ──────────────────────────────────────
const HQ_COMPLIANCE = [
  { id: 1, agency: 'Sakina Travels',  initials: 'ST', tone: '#6E4F7A', item: 'Ministry mutawwif licence', country: 'Canada',    state: 'review',  note: 'Document uploaded — awaiting reviewer', submitted: '6h ago' },
  { id: 2, agency: 'Ihsan Journeys',  initials: 'IJ', tone: '#9A6A3C', item: 'IATA / travel trade licence', country: 'Australia', state: 'awaiting', note: 'Reminder sent · trial converts in 11 days', submitted: '—' },
  { id: 3, agency: 'Safar Noor',      initials: 'SN', tone: '#8A6A2E', item: 'Annual re-verification',     country: 'UAE',        state: 'due',     note: 'Renewal due in 9 days', submitted: '—' },
  { id: 4, agency: 'Madinah Express', initials: 'ME', tone: '#4A5B6E', item: 'Ministry mutawwif licence',  country: 'Pakistan',   state: 'verified', note: 'Approved by Layla O.', submitted: '12 Apr' },
];
const HQ_COMPLIANCE_STATE = {
  review:   { label: 'Needs review', tone: 'var(--ug-gold)' },
  awaiting: { label: 'Awaiting docs', tone: '#5B7A9A' },
  due:      { label: 'Renewal due',   tone: '#C77D3A' },
  verified: { label: 'Verified',      tone: 'var(--ug-success)' },
};

// ── Recent invoices (revenue view) ───────────────────────────────────────
const HQ_INVOICES = [
  { id: 'INV-2044', agency: 'Baytullah Travel',  plan: 'enterprise', amount: 900, status: 'paid',     date: '18 Jun' },
  { id: 'INV-2043', agency: 'Al-Noor Tours',     plan: 'scale',      amount: 399, status: 'paid',     date: '18 Jun' },
  { id: 'INV-2042', agency: 'Anwar Hajj & Umrah',plan: 'scale',      amount: 399, status: 'paid',     date: '17 Jun' },
  { id: 'INV-2041', agency: 'Rahma Pilgrimage',  plan: 'scale',      amount: 399, status: 'paid',     date: '17 Jun' },
  { id: 'INV-2040', agency: 'Tayyibah Tours',    plan: 'growth',     amount: 149, status: 'overdue',  date: '04 Jun' },
  { id: 'INV-2039', agency: 'Barakah Tours',     plan: 'growth',     amount: 149, status: 'paid',     date: '15 Jun' },
  { id: 'INV-2038', agency: 'Firdaus Travel',    plan: 'growth',     amount: 149, status: 'paid',     date: '14 Jun' },
  { id: 'INV-2037', agency: 'Safar Noor',        plan: 'growth',     amount: 149, status: 'paid',     date: '12 Jun' },
  { id: 'INV-2036', agency: 'Madinah Express',   plan: 'starter',    amount: 49,  status: 'paid',     date: '10 Jun' },
];
const HQ_INVOICE_STATUS = {
  paid:    { label: 'Paid',    tone: 'var(--ug-success)' },
  overdue: { label: 'Overdue', tone: 'var(--ug-danger)' },
  pending: { label: 'Pending', tone: 'var(--ug-gold)' },
};

// ── Needs attention (derived signals → deep-link OUT to fix) ──────────────
// tool = which source tool to open to actually resolve it.
const HQ_ATTENTION = [
  { id: 1, sev: 'high',   agency: 'Tayyibah Tours',  initials: 'TT', tone: '#5B5B82',
    title: 'Invoice overdue 14 days', detail: 'Card declined ×2 · £149 Growth · subscription will cancel in 7 days', tool: 'stripe' },
  { id: 2, sev: 'high',   agency: 'Noor ul-Haram',   initials: 'NH', tone: 'var(--ug-muted)',
    title: 'Subscription canceled', detail: 'Canceled 4 days ago after 11 months · reason: switched to in-house tool', tool: 'stripe' },
  { id: 3, sev: 'medium', agency: 'Sakina Travels',  initials: 'ST', tone: '#6E4F7A',
    title: 'Trial converts in 6 days', detail: '18 pilgrims imported · licence not yet uploaded', tool: 'supabase' },
  { id: 4, sev: 'medium', agency: 'Firdaus Travel',  initials: 'FT', tone: '#3F6E6E',
    title: 'Usage dropped 60% this month', detail: 'No logins in 6 days · Growth · churn-risk', tool: 'supabase' },
  { id: 5, sev: 'low',    agency: 'Ihsan Journeys',  initials: 'IJ', tone: '#9A6A3C',
    title: 'Trial ends in 11 days', detail: 'Low activation · 22% feature adoption', tool: 'supabase' },
];

// ── Helpers ──────────────────────────────────────────────────────────────
function hqMoney(n) { return '£' + n.toLocaleString('en-GB'); }
function hqMoneyK(n) { return '£' + (n / 1000).toFixed(n % 1000 === 0 ? 0 : 1) + 'k'; }

function HqAvatar({ a, size = 38, radius = 11 }) {
  return (
    <div style={{
      width: size, height: size, borderRadius: radius, background: a.tone, color: '#fff',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontSize: size * 0.36, fontWeight: 600, flexShrink: 0, fontFamily: 'var(--ug-body)', letterSpacing: '0.01em',
    }}>{a.initials}</div>
  );
}

Object.assign(window, {
  HQ_USER, HQ_KPI, HQ_MRR_SERIES, HQ_PILGRIM_SERIES, HQ_MONTHS,
  HQ_PLANS, HQ_PLAN_MIX, HQ_STATUS, HQ_HEALTH, HQ_AGENCIES, HQ_SYNC, HQ_ATTENTION,
  HQ_ACTIVITY, HQ_TICKETS, HQ_PRIORITY, HQ_TICKET_STATUS,
  HQ_COMPLIANCE, HQ_COMPLIANCE_STATE, HQ_INVOICES, HQ_INVOICE_STATUS,
  hqMoney, hqMoneyK, HqAvatar,
});
