/* ========================================================================
   Placeholder data — Acme Co. / Lewis Mangrove
   ======================================================================== */

const COMPANY = "Acme Industries";
const CURRENT_USER = { full_name: "Lewis Mangrove", email: "lewis@acmeco.co.uk", initials: "LM", otp_enabled: false };

const USERS = [
  { id: 1, full_name: "Sarah Chen",      email: "sarah@acmeco.co.uk",  initials: "SC", sim_ids: [1] },
  { id: 2, full_name: "Jay Patel",       email: "jay@acmeco.co.uk",    initials: "JP", sim_ids: [2] },
  { id: 3, full_name: "Maya Rossi",      email: "maya@acmeco.co.uk",   initials: "MR", sim_ids: [3] },
  { id: 4, full_name: "Tom Williams",    email: "tom@acmeco.co.uk",    initials: "TW", sim_ids: [4] },
  { id: 5, full_name: "Priya Shah",      email: "priya@acmeco.co.uk",  initials: "PS", sim_ids: [5] },
  { id: 6, full_name: "Daniel O'Connor", email: "daniel@acmeco.co.uk", initials: "DO", sim_ids: [6] },
  { id: 7, full_name: "Helena Brandt",   email: "helena@acmeco.co.uk", initials: "HB", sim_ids: [7] },
  { id: 8, full_name: "Marcus Webb",     email: "marcus@acmeco.co.uk", initials: "MW", sim_ids: [8] },
  { id: 9, full_name: "Lewis Mangrove",  email: "lewis@acmeco.co.uk",  initials: "LM", sim_ids: [9, 10] },
  { id: 10, full_name: "Ade Okonjo",     email: "ade@acmeco.co.uk",    initials: "AO", sim_ids: [] },
  { id: 11, full_name: "Iris Lin",       email: "iris@acmeco.co.uk",   initials: "IL", sim_ids: [] },
  // SIM holders with no administrative access
  { id: 12, full_name: "Beatrice Adeyemi", email: "beatrice.adeyemi@acmeco.co.uk", initials: "BA", sim_ids: [11] },
  { id: 13, full_name: "Callum Frost",     email: "callum.frost@acmeco.co.uk",     initials: "CF", sim_ids: [12] },
  { id: 14, full_name: "Elena Vasquez",    email: "elena.vasquez@acmeco.co.uk",    initials: "EV", sim_ids: [] },
  { id: 15, full_name: "Grace Mensah",     email: "grace.mensah@acmeco.co.uk",     initials: "GM", sim_ids: [] },
  { id: 16, full_name: "Nathan Boyd",      email: "nathan.boyd@acmeco.co.uk",      initials: "NB", sim_ids: [] },
  { id: 17, full_name: "Ravi Chandra",     email: "ravi.chandra@acmeco.co.uk",     initials: "RC", sim_ids: [] },
];

/* The rest of the workforce. Generated so the estate can grow without three
   hundred lines of literals, and so ids and initials cannot drift apart. */
const EXTRA_PEOPLE = [
  "Aisha Bello", "Hugo Lindqvist", "Leila Haddad", "Bruno Ferreira", "Chloe Duarte",
  "Ismail Toure", "Freya Sandberg", "Kenji Watanabe", "Rosa Delgado", "Tobias Reinhardt",
  "Amara Nwosu", "Vikram Desai", "Nina Halvorsen", "Owen Pryce", "Sofia Marchetti",
  "Rafael Santos", "Yusuf Karim", "Marta Kowalska", "Dylan Reeve", "Esther Kimani",
  "Lorenzo Bianchi", "Saoirse Byrne", "Mateo Alvarez", "Anouk Verhoeven", "Tariq Mansour",
  "Greta Fischer", "Joon-ho Park", "Camille Rousseau", "Felix Andersson", "Priscilla Osei",
  "Dimitri Volkov", "Hannah Whitfield", "Omar Farouk", "Ingrid Bakken", "Samuel Adeyinka"];
const slug = (n) => n.toLowerCase().replace(/[^a-z ]/g, "").split(" ").join(".");
const initialsOf = (n) => { const p = n.split(" "); return (p[0][0] + p[p.length - 1][0]).toUpperCase(); };
EXTRA_PEOPLE.forEach((name, i) => {
  USERS.push({ id: 100 + i, full_name: name, email: slug(name) + "@acmeco.co.uk", initials: initialsOf(name), sim_ids: [] });
});

const PLANS = {
  unlimited: { name: "Unlimited", data: "Unlimited", limit_gb: null,  price: 28 },
  big:       { name: "25 GB",     data: "25 GB",     limit_gb: 25,    price: 18 },
  small:     { name: "5 GB",      data: "5 GB",      limit_gb: 5,     price: 12 },
};

const SIMS = [
  // One example per status at top so every visual treatment is visible on page 1
  { id: 1,  serial: "8944501912340002101", msisdn: "+44 7700 900021", status: "Active",         sim_type: "eSIM",     plan: "unlimited", used: 4.7, data_active: true,  activation_date: "2024-08-12" },
  { id: 8,  serial: "8944501912340002108", msisdn: "+44 7700 900145", status: "Activated",      sim_type: "eSIM",     plan: "big",       used: 1.9,  data_active: true,  activation_date: "2025-04-22" },
  { id: 10, serial: "8944501912340002110", msisdn: "+44 7700 900174", status: "For Activation", sim_type: "Physical", plan: "small",     used: 0.1,  data_active: false, activation_date: null },
  { id: 6,  serial: "8944501912340002106", msisdn: "+44 7700 900113", status: "Suspended",      sim_type: "Physical", plan: "unlimited", used: 0.1,  data_active: false, activation_date: "2024-10-01" },
  { id: 13, serial: "8944501912340002113", msisdn: "+44 7700 900219", status: "Terminated",     sim_type: "Physical", plan: "small",     used: 0.1,  data_active: false, activation_date: "2023-11-30" },
  // Remaining
  { id: 2,  serial: "8944501912340002102", msisdn: "+44 7700 900012", status: "Active",     sim_type: "eSIM",     plan: "unlimited", used: 48.6, data_active: true,  activation_date: "2024-09-03" },
  { id: 3,  serial: "8944501912340002103", msisdn: "+44 7700 900044", status: "Active",     sim_type: "Physical", plan: "big",       used: 7.5, data_active: true,  activation_date: "2025-01-20" },
  { id: 4,  serial: "8944501912340002104", msisdn: "+44 7700 900067", status: "Active",     sim_type: "Physical", plan: "small",     used: 0.1,  data_active: true,  activation_date: "2024-11-04" },
  { id: 5,  serial: "8944501912340002105", msisdn: "+44 7700 900088", status: "Active",     sim_type: "eSIM",     plan: "big",       used: 7, data_active: true,  activation_date: "2025-02-14" },
  { id: 7,  serial: "8944501912340002107", msisdn: "+44 7700 900129", status: "Active",     sim_type: "eSIM",     plan: "unlimited", used: 10.4, data_active: true,  activation_date: "2025-03-09" },
  { id: 9,  serial: "8944501912340002109", msisdn: "+44 7700 900158", status: "Active",     sim_type: "Physical", plan: "unlimited", used: 3.1,  data_active: true,  activation_date: "2024-07-18" },
  { id: 11, serial: "8944501912340002111", msisdn: "+44 7700 900192", status: "Active",     sim_type: "eSIM",     plan: "big",       used: 3.9, data_active: true,  activation_date: "2025-05-30" },
  { id: 12, serial: "8944501912340002112", msisdn: "+44 7700 900208", status: "Active",     sim_type: "Physical", plan: "unlimited", used: 7.2, data_active: true,  activation_date: "2024-12-10" },
  // A few more on their own plans rather than the pool
  { id: 14, serial: "8944501912340002114", msisdn: "+44 7700 900231", status: "Active",     sim_type: "Physical", plan: "small",     used: 4.85, data_active: true,  activation_date: "2025-02-02" },
  { id: 15, serial: "8944501912340002115", msisdn: "+44 7700 900244", status: "Active",     sim_type: "eSIM",     plan: "small",     used: 0.6,  data_active: true,  activation_date: "2025-02-02" },
  { id: 16, serial: "8944501912340002116", msisdn: "+44 7700 900257", status: "Activated",  sim_type: "Physical", plan: "small",     used: 0.1,  data_active: true,  activation_date: "2025-05-15" },
  /* Unassigned inventory, waiting for people. Twelve eSIMs and eight physical
     SIMs, so bulk assignment and its activation emails can be demonstrated. */
  { id: 20, serial: "8944501912340002220", msisdn: "+44 7700 91000", status: "For Activation", sim_type: "eSIM", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 21, serial: "8944501912340002221", msisdn: "+44 7700 91007", status: "For Activation", sim_type: "eSIM", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 22, serial: "8944501912340002222", msisdn: "+44 7700 91014", status: "For Activation", sim_type: "eSIM", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 23, serial: "8944501912340002223", msisdn: "+44 7700 91021", status: "For Activation", sim_type: "eSIM", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 24, serial: "8944501912340002224", msisdn: "+44 7700 91028", status: "For Activation", sim_type: "eSIM", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 25, serial: "8944501912340002225", msisdn: "+44 7700 91035", status: "For Activation", sim_type: "eSIM", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 26, serial: "8944501912340002226", msisdn: "+44 7700 91042", status: "For Activation", sim_type: "eSIM", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 27, serial: "8944501912340002227", msisdn: "+44 7700 91049", status: "For Activation", sim_type: "eSIM", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 28, serial: "8944501912340002228", msisdn: "+44 7700 91056", status: "For Activation", sim_type: "eSIM", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 29, serial: "8944501912340002229", msisdn: "+44 7700 91063", status: "For Activation", sim_type: "eSIM", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 30, serial: "8944501912340002230", msisdn: "+44 7700 91070", status: "For Activation", sim_type: "eSIM", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 31, serial: "8944501912340002231", msisdn: "+44 7700 91077", status: "For Activation", sim_type: "eSIM", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 32, serial: "8944501912340002232", msisdn: "+44 7700 91084", status: "For Activation", sim_type: "Physical", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 33, serial: "8944501912340002233", msisdn: "+44 7700 91091", status: "For Activation", sim_type: "Physical", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 34, serial: "8944501912340002234", msisdn: "+44 7700 91098", status: "For Activation", sim_type: "Physical", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 35, serial: "8944501912340002235", msisdn: "+44 7700 91105", status: "For Activation", sim_type: "Physical", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 36, serial: "8944501912340002236", msisdn: "+44 7700 91112", status: "For Activation", sim_type: "Physical", plan: "small", used: 0, data_active: false, activation_date: null },
  { id: 37, serial: "8944501912340002237", msisdn: "+44 7700 91119", status: "For Activation", sim_type: "Physical", plan: "unlimited", used: 0, data_active: false, activation_date: null },
  { id: 38, serial: "8944501912340002238", msisdn: "+44 7700 91126", status: "For Activation", sim_type: "Physical", plan: "big", used: 0, data_active: false, activation_date: null },
  { id: 39, serial: "8944501912340002239", msisdn: "+44 7700 91133", status: "For Activation", sim_type: "Physical", plan: "small", used: 0, data_active: false, activation_date: null },
];

/* ---- The working estate ----
   Thirty-four more SIMs in service, assigned to the wider workforce. Usage is
   deterministic so a demo looks the same twice, and every activation date is
   computed backwards from today so nothing goes stale. */
const EST_NOW = new Date();
const backDate = (days) => {
  const x = new Date(EST_NOW.getFullYear(), EST_NOW.getMonth(), EST_NOW.getDate() - days);
  return x.toISOString().slice(0, 10);
};
const WORKING_USED = [3.2, 8.7, 1.4, 12.6, 5.9, 0.8, 7.3, 2.1, 14.8, 4.4, 9.6, 1.9, 6.2, 11.3,
  0.4, 8.1, 3.7, 5.5, 13.9, 2.8, 7.8, 1.2, 10.7, 4.9, 6.6, 0.6, 9.1, 3.4, 12.1, 2.4, 8.4, 5.1, 1.7, 7.0];
const WORKING_TYPE = ["eSIM", "Physical", "eSIM", "eSIM", "Physical", "Physical", "eSIM", "Physical",
  "eSIM", "eSIM", "Physical", "eSIM", "eSIM", "eSIM", "Physical", "Physical", "eSIM", "eSIM",
  "Physical", "eSIM", "eSIM", "Physical", "Physical", "eSIM", "eSIM", "eSIM", "Physical", "eSIM",
  "eSIM", "Physical", "eSIM", "eSIM", "Physical", "Physical"];
/* A handful sit outside the pool on their own plans. */
const WORKING_INDIVIDUAL = new Set([5, 11, 17, 21, 25, 32]);
WORKING_USED.forEach((used, i) => {
  const id = 40 + i;
  const person = USERS.find((u) => u.id === 100 + i);
  const type = WORKING_TYPE[i];
  const individual = WORKING_INDIVIDUAL.has(i);
  const sim = {
    id,
    serial: "89445019123400024" + String(10 + i),
    msisdn: "+44 7700 9" + String(2000 + i * 13).slice(0, 5),
    status: "Active",
    sim_type: type,
    plan: i % 5 === 0 ? "unlimited" : i % 3 === 0 ? "small" : "big",
    used,
    data_active: true,
    activation_date: backDate(40 + i * 11)
  };
  if (individual) sim.group = "individual";
  SIMS.push(sim);
  if (person) person.sim_ids.push(id);
});

/* =====================================================================
   ALERTS
   - kind drives both icon + background tone (see ALERT_KINDS map)
   - severity drives High/Low Priority summary counts
   - Real Transatel webhook events (1–14) listed first, then non-Transatel
     placeholder alerts kept for future use (roaming, billing, etc.)
   ===================================================================== */
const ALERT_KINDS = {
  /* ---- Estate expansion (Stories 1 to 7) ---- */
  sim_request_open:    { icon: "loader",         tone: "clay",   severity: "low"  },
  sim_request_done:    { icon: "circle-check",   tone: "mist",   severity: "low"  },
  sim_request_declined:{ icon: "triangle-alert", tone: "ember",  severity: "high" },
  usage_available:     { icon: "chart-column",   tone: "cream",  severity: "low"  },
  esim_installed:      { icon: "circle-check",   tone: "mist",   severity: "low"  },
  sims_assigned:       { icon: "circle-check",   tone: "mist",   severity: "low"  },
  users_imported:      { icon: "users-round",    tone: "cream",  severity: "low"  },
  /* ---- SIM lifecycle ---- */
  sim_activated:             { icon: "circle-check",       tone: "mist",   severity: "low"  },
  sim_reactivated:           { icon: "circle-play",        tone: "mist",   severity: "low"  },
  sim_suspended:             { icon: "circle-pause",       tone: "clay",   severity: "high" },
  sim_terminated:            { icon: "power-off",          tone: "ember",  severity: "high" },
  sim_replaced:              { icon: "arrow-right-left",   tone: "cream",  severity: "low"  },
  /* ---- Keeping a number ---- */
  number_transfer_started:   { icon: "loader",             tone: "clay",   severity: "low"  },
  number_transfer_complete:  { icon: "circle-check",       tone: "mist",   severity: "low"  },
  number_transfer_failed:    { icon: "triangle-alert",     tone: "ember",  severity: "high" },
  /* ---- Usage, plans and billing ---- */
  data_threshold:            { icon: "triangle-alert",     tone: "clay",   severity: "high" },
  roaming_detected:          { icon: "plane",              tone: "cream",  severity: "low"  },
  plan_upgraded:             { icon: "circle-arrow-up",    tone: "cream",  severity: "low"  },
  invoice_available:         { icon: "file-text",          tone: "cream",  severity: "low"  },
  invoice_paid:              { icon: "circle-check",       tone: "mist",   severity: "low"  },
};

const ALERTS = [
  { id: 1,  kind: "sim_activated",     title: "SIM activated",            description: "SIM 8944501912340002108 is active and ready to use.",                                  time_ago: "12 minutes" },
  { id: 2,  kind: "data_threshold",    title: "Data threshold reached",   description: "SIM 8944501912340002114 has used 97% of its 5 GB plan allowance this billing period.",  time_ago: "2 hours" },
  { id: 3,  kind: "sim_suspended",     title: "SIM suspended",            description: "SIM 8944501912340002106 has been suspended and cannot be used.",                       time_ago: "3 hours" },
  { id: 4,  kind: "sim_activated",     title: "SIM activated",            description: "SIM 8944501912340002110 is active and ready to use.",                                  time_ago: "6 hours" },
  { id: 5,  kind: "invoice_available", title: "Invoice available",        description: "Invoice #1248 is ready. View it in Subscription and Billing.",                          time_ago: "Yesterday" },
  { id: 6,  kind: "sim_reactivated",   title: "SIM reactivated",          description: "SIM 8944501912340002104 is active again and ready to use.",                             time_ago: "Yesterday" },
  { id: 7,  kind: "number_transfer_started",  title: "Number transfer started",  description: "The transfer of +44 7700 900301 to SIM 8944501912340002112 is in progress.",     time_ago: "2 days" },
  { id: 8,  kind: "number_transfer_complete", title: "Number transfer complete", description: "+44 7700 900277 has transferred to SIM 8944501912340002111.",                    time_ago: "3 days" },
  { id: 9,  kind: "sim_replaced",      title: "SIM replaced",             description: "SIM 8944501912340002109 has been replaced and the new SIM is ready to use.",            time_ago: "4 days" },
  { id: 10, kind: "sim_terminated",    title: "SIM terminated",           description: "SIM 8944501912340002113 has been terminated and can no longer be used.",                time_ago: "5 days" },
  { id: 11, kind: "number_transfer_failed", title: "Number transfer could not be completed", description: "The transfer of +44 7700 900288 was rejected by the previous provider. Contact support to try again.", time_ago: "6 days" },
  { id: 12, kind: "roaming_detected",  title: "Roaming detected",         description: "Roaming activity detected on +44 7700 900012 in France. EU roaming is included at no extra cost.", time_ago: "1 week" },
  { id: 13, kind: "plan_upgraded",     title: "Plan upgraded",            description: "SIM 8944501912340002105 was upgraded to the 25 GB plan.",                               time_ago: "2 weeks" },
  { id: 14, kind: "invoice_paid",      title: "Invoice paid",             description: "Invoice #1247 was paid by direct debit.",                                              time_ago: "2 weeks" },
];


const SUB_NOW = new Date();

const subDate = (monthsAhead, day) => new Date(SUB_NOW.getFullYear(), SUB_NOW.getMonth() + monthsAhead, day)
  .toLocaleDateString("en-GB", { day: "numeric", month: "long", year: "numeric" });
const SUBSCRIPTIONS = [
  { id: 4821, stripe_status: "active",   sim_ids: [1, 2, 5, 7, 8, 11], next_bill: subDate(1, 12), expires: subDate(11, 10) },
  { id: 4699, stripe_status: "active",   sim_ids: [3, 4, 9, 12],       next_bill: subDate(1, 20), expires: subDate(19, 18) },
  { id: 4502, stripe_status: "trialing", sim_ids: [10],                next_bill: subDate(1, 5),  expires: "—" },
  { id: 4744, stripe_status: "active",   sim_ids: [],                  next_bill: subDate(1, 26), expires: subDate(15, 26) },
  { id: 4790, stripe_status: "active",   sim_ids: [],                  next_bill: subDate(1, 3),  expires: subDate(23, 3) },
];

/* The wider workforce sits on one line and unassigned stock on another, since
   stock is ordered as a batch rather than per person. Assigning by rule rather
   than by hand means a SIM added later cannot be missed. */
(function assignSubscriptions() {
  const onSub = new Set(SUBSCRIPTIONS.reduce((a, s) => a.concat(s.sim_ids), []));
  const line = (id) => SUBSCRIPTIONS.find((s) => s.id === id);
  SIMS.forEach((s) => {
    if (onSub.has(s.id)) return;
    if (s.status === "For Activation") line(4502).sim_ids.push(s.id);
    else if (!USERS.some((u) => u.sim_ids.includes(s.id))) line(4790).sim_ids.push(s.id);
    else line(4744).sim_ids.push(s.id);
  });
})();

const CO2 = {
  net_emissions: 18.42,        // kg CO2e this period
  prev_period: 21.7,
  offset_pct: 100,
  /* The eSIM figures are counted at render time in Co2OverviewTab, not stored
     here: a load-time snapshot depends on seed-file order and goes stale the
     moment a travel eSIM is bought during a session. */
  esims: null,
  physical: null,
  esim_adoption: null,
  projects: 12,
  // 12 months of values, kg CO2e
  trend: [12.1, 13.4, 14.0, 15.2, 16.1, 15.5, 17.0, 16.7, 18.0, 19.1, 18.8, 18.42],
  categories: [
    { label: "Data usage",  value: 11.2, color: "#1e4136" },
    { label: "Voice + Data", value: 3.1,  color: "#256751" },
    { label: "Roaming",     value: 2.4,  color: "#509a82" },
    { label: "Hardware",    value: 1.0,  color: "#94ceb7" },
    { label: "Logistics",   value: 0.72, color: "#D2FF85" },
  ],
};

const FAQ = [
  {
    title: "Getting Started",
    elements: [
      { q: "What is the platform?", a: "It's Meaningful Planet's self-service platform for managing your organisation's mobile connectivity: SIMs, users, plans, billing and environmental impact, all in one place." },
      { q: "How do I activate my first SIM?", a: "Order a SIM from the SIM Management page, assign a user, and follow the activation guide that's emailed to them. Physical SIMs typically arrive within 2 working days; eSIMs activate instantly via QR code." },
      { q: "Can I transfer an existing number to a Meaningful Planet SIM?", a: "Yes. From the SIM Management page, choose “Transfer Your Number” on the relevant SIM and follow the PAC/STAC process." },
    ],
  },
  {
    title: "Plans & Billing",
    elements: [
      { q: "What plans are available?", a: "We currently offer 5 GB, 25 GB and Unlimited data plans. EU roaming is included at no extra cost." },
      { q: "When am I billed?", a: "We bill monthly via Stripe. You can view all invoices and update payment methods from the Subscription & Billing page." },
      { q: "Can I upgrade or downgrade individual SIMs?", a: "Yes. Each SIM in a subscription can be upgraded individually. Open a subscription and choose 'Upgrade' on the SIM you want to change." },
    ],
  },
  {
    title: "Environmental Impact",
    elements: [
      { q: "How do you calculate emissions?", a: "Carbon emissions are calculated using verified emission factors based on actual usage — data volume, voice minutes, hardware lifecycle and logistics." },
      { q: "How is my mobile connectivity carbon neutral?", a: "We invest 10% of profits into verified UK nature-based projects which offset 100% of your fleet's emissions over the following 12 months." },
    ],
  },
];

const PARTNER_CATEGORIES = [
  { value: "all",       label: "All" },
  { value: "carbon",    label: "Carbon reporting & verification" },
  { value: "utilities", label: "Utilities" },
  { value: "it",        label: "IT & software" },
  { value: "benefits",  label: "Employee benefits" },
];

window.MP_ALLOW = (cap) => !window.MP_CAN || window.MP_CAN(cap);

window.MILO_DATA = { COMPANY, CURRENT_USER, USERS, PLANS, SIMS, ALERTS, ALERT_KINDS, SUBSCRIPTIONS, CO2, FAQ };
