// Mock data for the VaultOS prototype.
// Designed around an NZ self-storage facility: "Auckland Central" with 5 zones.

const FIRST_NAMES = ["Aroha","Te Rangi","Mei","Hemi","Olivia","Liam","Noa","Aria","Tama","Anika","Connor","Ruby","Zane","Mila","Ari","Pita","Hana","Kahu","Charlotte","Henry","Sienna","Jack","Ines","Ravi","Yuki","Ben","Nia","Eli","Pia","Tane"];
const LAST_NAMES = ["Whetu","Williams","Chen","Patel","Brown","Singh","Tane","Ngata","O'Connor","Walker","Lee","Smith","Park","Karaka","Rao","Wright","Tahere","Robinson","Manu","Ito","Murphy","Te Awa","Ngāpō","Adams","Clark"];

const seed = (s) => () => {
  s = (s * 9301 + 49297) % 233280;
  return s / 233280;
};

const rand = seed(42);
const pick = (arr) => arr[Math.floor(rand() * arr.length)];

// Zones: A,B,C are interior climate-controlled; D is exterior drive-up; E is locker / small.
const ZONE_DEFS = [
  { id: "A", label: "Zone A", climate: true,  type: "Standard",   rows: 3, cols: 12, sizeW: [1.5, 2.4], pos: { x: 40,  y: 60,  w: 360, h: 200 } },
  { id: "B", label: "Zone B", climate: true,  type: "Climate",    rows: 3, cols: 10, sizeW: [2.4, 3.0], pos: { x: 420, y: 60,  w: 320, h: 200 } },
  { id: "C", label: "Zone C", climate: false, type: "Large",      rows: 2, cols: 8,  sizeW: [3.0, 4.5], pos: { x: 760, y: 60,  w: 280, h: 140 } },
  { id: "D", label: "Zone D", climate: false, type: "Drive-up",   rows: 1, cols: 14, sizeW: [3.0, 4.5], pos: { x: 40,  y: 290, w: 720, h: 100 } },
  { id: "E", label: "Zone E", climate: true,  type: "Locker",     rows: 4, cols: 10, sizeW: [1.0, 1.5], pos: { x: 780, y: 220, w: 260, h: 170 } },
];

const buildUnits = () => {
  const units = [];
  let i = 0;
  for (const z of ZONE_DEFS) {
    for (let r = 0; r < z.rows; r++) {
      for (let c = 0; c < z.cols; c++) {
        const id = `${z.id}-${String(r * z.cols + c + 101).padStart(3, "0")}`;
        const r1 = rand();
        let state =
          r1 < 0.12 ? "vacant" :
          r1 < 0.18 ? "reserved" :
          r1 < 0.22 ? "delinq" :
          r1 < 0.24 ? "alarm" :
          r1 < 0.26 ? "open" :
          r1 < 0.32 ? "locked" :
          "occupied";
        // small zones bias toward more vacancies
        if (z.id === "E" && rand() < 0.25) state = "vacant";
        const widthM = (z.sizeW[0] + rand() * (z.sizeW[1] - z.sizeW[0])).toFixed(1);
        const lengthM = (parseFloat(widthM) * (1.6 + rand() * 0.8)).toFixed(1);
        const sqm = (parseFloat(widthM) * parseFloat(lengthM)).toFixed(1);
        const price = Math.round((parseFloat(sqm) * (z.climate ? 38 : 28) + 40) / 5) * 5;
        units.push({
          id, zone: z.id, row: r, col: c,
          state,
          widthM, lengthM, sqm,
          climate: z.climate,
          type: z.type,
          price,
          tenant: state === "occupied" || state === "delinq" || state === "alarm" || state === "locked"
            ? `${pick(FIRST_NAMES)} ${pick(LAST_NAMES)}` : null,
        });
        i++;
      }
    }
  }
  return units;
};

const UNITS = buildUnits();
const ZONES = ZONE_DEFS;

// Recent activity / event feed
const ACTIVITY = [
  { t: "08:42", type: "gate-in",   actor: "Hemi Tane",       unit: "B-118", note: "Gate 1 entry — keypad",            tone: "info" },
  { t: "08:39", type: "ai",        actor: "VaultOS AI",      unit: null,    note: "Smart pricing: +4% suggested for Zone B",  tone: "violet" },
  { t: "08:31", type: "payment",   actor: "Aroha Whetu",     unit: "A-104", note: "$320.00 NZD — Direct Debit",        tone: "ok" },
  { t: "08:24", type: "alarm",     actor: "Door sensor",     unit: "C-208", note: "Door open — no scheduled access",   tone: "bad" },
  { t: "08:11", type: "move-in",   actor: "Olivia Brown",    unit: "E-403", note: "New 30-day lease signed",           tone: "ok" },
  { t: "07:58", type: "delinq",    actor: "System",          unit: "D-302", note: "60-day overdue — gate suspended",   tone: "bad" },
  { t: "07:42", type: "gate-out",  actor: "Mei Chen",        unit: "B-115", note: "Gate 2 exit — mobile unlock",       tone: "info" },
  { t: "07:30", type: "lock",      actor: "Smart lock 4F",   unit: "A-102", note: "Battery 18% — replace soon",        tone: "warn" },
  { t: "07:14", type: "ai",        actor: "VaultOS AI",      unit: null,    note: "12 climate units likely vacant in May", tone: "violet" },
  { t: "06:58", type: "move-out",  actor: "Liam Walker",     unit: "C-205", note: "Notice given — vacate by May 15",   tone: "info" },
  { t: "06:40", type: "payment",   actor: "Anika Rao",       unit: "B-122", note: "$185.00 NZD — Apple Pay",           tone: "ok" },
  { t: "06:22", type: "gate-in",   actor: "Connor O'Connor", unit: "D-308", note: "Vehicle gate — license plate read", tone: "info" },
];

// Top delinquent accounts
const DELINQUENTS = [
  { name: "Liam Walker",   unit: "D-302", days: 64, amount: 720, last: "Mar 4",  contact: 4, risk: "high"   },
  { name: "Mila Patel",    unit: "A-109", days: 38, amount: 410, last: "Apr 1",  contact: 2, risk: "high"   },
  { name: "Zane Smith",    unit: "B-114", days: 22, amount: 260, last: "Apr 17", contact: 1, risk: "medium" },
  { name: "Ari Karaka",    unit: "C-201", days: 14, amount: 180, last: "Apr 25", contact: 1, risk: "medium" },
  { name: "Yuki Ito",      unit: "E-411", days:  8, amount: 105, last: "Apr 30", contact: 0, risk: "low"    },
];

// Today's bookings / move-ins
const BOOKINGS = [
  { time: "10:30", name: "Sienna Wright",   unit: "B-117", type: "Move-in",   stage: "ID verified" },
  { time: "11:00", name: "Ravi Singh",      unit: "A-105", type: "Move-in",   stage: "Lease pending" },
  { time: "13:15", name: "Jack Murphy",     unit: "D-307", type: "Tour",      stage: "Confirmed" },
  { time: "14:00", name: "Ines Te Awa",     unit: "E-405", type: "Move-out",  stage: "Final inspection" },
  { time: "15:30", name: "Pita Ngata",      unit: "C-203", type: "Move-in",   stage: "Awaiting deposit" },
];

// Multi-site portfolio
const SITES = [
  { id: "akl-c", city: "Auckland Central", units: 142, occ: 92, revPam: 38.4, deltaOcc: 1.8, alerts: 3, climate: true,  trend: [62,64,66,70,74,78,82,86,88,90,91,92] },
  { id: "akl-n", city: "North Shore",      units: 96,  occ: 88, revPam: 35.1, deltaOcc: 0.4, alerts: 1, climate: true,  trend: [70,72,73,75,76,78,80,82,84,85,86,88] },
  { id: "wgn",   city: "Wellington CBD",   units: 78,  occ: 84, revPam: 41.0, deltaOcc: 2.1, alerts: 0, climate: true,  trend: [60,62,65,68,70,73,75,78,80,82,83,84] },
  { id: "chc",   city: "Christchurch",     units: 110, occ: 79, revPam: 30.2, deltaOcc: -0.6, alerts: 2, climate: false, trend: [82,80,79,78,79,80,79,80,79,80,79,79] },
  { id: "queens",city: "Queenstown",       units: 54,  occ: 96, revPam: 52.1, deltaOcc: 3.2, alerts: 0, climate: true,  trend: [70,75,80,82,85,88,90,92,93,94,95,96] },
  { id: "ham",   city: "Hamilton",         units: 88,  occ: 71, revPam: 26.8, deltaOcc: -1.2, alerts: 4, climate: false, trend: [78,76,75,74,73,72,71,72,71,71,71,71] },
];

// AI nudges
const AI_NUDGES = [
  { kind: "pricing",  title: "Raise Zone B 2.4×3.0m by 4%",  body: "Competitor 'Storage King Newmarket' raised similar rates by 7% on Apr 28. Forecast occupancy impact: −0.3%. Annualized lift: +$4,820.", confidence: 0.86 },
  { kind: "vacancy",  title: "12 climate units likely vacant in May", body: "Pattern matched on lease length, payment cadence and access frequency. Suggest pre-listing at 92% rate.", confidence: 0.74 },
  { kind: "ops",      title: "Smart lock A-102 battery low", body: "Predicted failure within 9 days based on historic decay curve. Auto-ticket created for technician.", confidence: 0.91 },
  { kind: "churn",    title: "3 tenants showing churn signals", body: "Reduced access frequency + 2 missed reminders. Recommend retention message with $25 credit.", confidence: 0.69 },
];

window.VAULT_DATA = { UNITS, ZONES, ACTIVITY, DELINQUENTS, BOOKINGS, SITES, AI_NUDGES };
