/* global React */
const { useState: useStateD, useMemo: useMemoD } = React;

// ===== Mock data (Thai) =====

const THAI_FIRST = ["สมชาย","สมหญิง","วิภา","ปรีชา","ธนพล","ณัฐ","อรอุมา","ภาณุ","กมล","พิมพ์","ปรีดา","สุวิทย์","เสาวลักษณ์","วราภรณ์","กิตติ","ชลธิชา","ศุภชัย","อภิญญา","ธีรวัฒน์","กัลยา","ปิยะ","สมศักดิ์","นภาพร","อาทิตย์","วรินทร","ปนัดดา","พงศกร","อนุชา","วริสรา","ชาญชัย","ปานเทพ","มาลี","สุดา","ภัทรา","วิชัย"];
const THAI_LAST = ["ใจดี","ทองคำ","ศรีสุข","แสงทอง","ภูมิรัตน์","พงษ์สวัสดิ์","วงศ์สว่าง","ศรีสะอาด","สวัสดิ์ภาพ","พรหมมินทร์","บุญมา","แก้วใส","คำหล้า","ธรรมรักษ์","สุขสมบูรณ์","เจริญรัตน์","วัฒนะ","อินทรประเสริฐ","มั่นคง","ดวงดี","พรรณา","แสงเดือน","คงคา","ปัญญาดี","ภักดี"];
const pick = (arr, i) => arr[i % arr.length];
const rand = (seed) => {
  let s = (seed * 2654435761) >>> 0;
  const step = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return s / 4294967296; };
  // warm-up so close seeds don't share their first call
  step(); step(); step();
  return step;
};

function makeName(i) {
  return `${pick(THAI_FIRST, i)} ${pick(THAI_LAST, Math.floor(i / 3) + (i*7))}`;
}

function makePhone(i) {
  const r = rand(i + 1000);
  return `08${Math.floor(r()*10)}-${String(Math.floor(r()*900+100))}-${String(Math.floor(r()*9000+1000))}`;
}

function makePlate(i) {
  const letters = "กขคงจฉชญฎฐผพฟภมยรลวศสห";
  const r = rand(i + 200);
  const a = letters[Math.floor(r()*letters.length)];
  const b = letters[Math.floor(r()*letters.length)];
  return `${Math.floor(r()*7+1)}${a}${b} ${Math.floor(r()*9000+1000)}`;
}

// ====== Generate 100 rooms across 5 floors, 20 per floor ======
function generateRooms() {
  const rooms = [];
  for (let f = 1; f <= 5; f++) {
    for (let n = 1; n <= 20; n++) {
      const no = `${f}${String(n).padStart(2,"0")}`;
      const r = rand(parseInt(no) + 17);
      // ~70% monthly, ~25% daily-style, 5% mixed
      const type = r() < 0.7 ? "monthly" : "daily";
      // For monthly: 75% occupied, 15% vacant, 10% overdue
      // For daily: 40% occupied today, 55% vacant, 5% cleaning
      let status, customer = null, rent;
      if (type === "monthly") {
        const baseRent = [3800, 4200, 4500, 4800, 5200][f-1] + (n % 4 === 0 ? 500 : 0);
        rent = baseRent;
        const rr = r();
        if (rr < 0.74) status = "occupied";
        else if (rr < 0.89) status = "vacant";
        else status = "overdue";
      } else {
        rent = 550 + (f * 30) + (n % 5 === 0 ? 100 : 0);
        const rr = r();
        if (rr < 0.42) status = "daily";
        else if (rr < 0.96) status = "vacant";
        else status = "cleaning";
      }
      if (status !== "vacant" && status !== "cleaning") {
        const idx = parseInt(no);
        customer = {
          id: `C${1000 + idx}`,
          name: makeName(idx),
          phone: makePhone(idx),
          since: type === "monthly" ? `2024-${String((idx % 12) + 1).padStart(2,"0")}-${String((idx % 28) + 1).padStart(2,"0")}` : `2026-05-${String(((idx % 9) + 1)).padStart(2,"0")}`,
          checkoutPlan: type === "daily" ? `2026-05-${String(((idx % 9) + 5)).padStart(2,"0")}` : null,
          deposit: type === "monthly" ? rent * 2 : 0,
        };
      }
      rooms.push({
        no, floor: f, type, rent, status, customer,
        size: n % 4 === 0 ? "ห้องใหญ่ 28 ตร.ม." : "ห้องมาตรฐาน 22 ตร.ม.",
        waterRate: 25,
        electricRate: 8,
        extras: [],
        prevMeter: { water: 100 + (parseInt(no) % 80), electric: 1000 + (parseInt(no) % 300) },
        currMeter: { water: 100 + (parseInt(no) % 80) + Math.floor(rand(parseInt(no))()*12+2), electric: 1000 + (parseInt(no) % 300) + Math.floor(rand(parseInt(no)+5)()*80+30) },
      });
    }
  }
  return rooms;
}

// ====== Generate 100 parking spots: Zone A,B,C,D each 25 ======
function generateParking() {
  const spots = [];
  const zones = ["A","B","C","D"];
  zones.forEach((z, zi) => {
    for (let n = 1; n <= 25; n++) {
      const no = `${z}${String(n).padStart(2,"0")}`;
      const r = rand(zi * 100 + n + 333);
      const rr = r();
      let status, customer = null, plate = null, type = "monthly", monthly = 1500 + (zi*200), endDate = null;
      if (rr < 0.55) { status = "occupied"; }
      else if (rr < 0.7) { status = "daily"; type = "daily"; }
      else if (rr < 0.78) { status = "overdue"; }
      else if (rr < 0.82) { status = "reserved"; }
      else { status = "vacant"; }
      if (status !== "vacant" && status !== "reserved") {
        const idx = zi*100+n;
        customer = {
          id: `P${500 + idx}`,
          name: makeName(idx + 19),
          phone: makePhone(idx + 7),
          plate: makePlate(idx),
        };
        plate = customer.plate;
        if (status === "daily") {
          endDate = "2026-05-12";
          monthly = 80; // daily rate
        } else {
          endDate = `2026-${String(((idx % 6) + 5)).padStart(2,"0")}-${String((idx % 28) + 1).padStart(2,"0")}`;
        }
      }
      spots.push({ no, zone: z, status, type, customer, plate, monthly, endDate, size: n % 6 === 0 ? "ใหญ่" : "มาตรฐาน" });
    }
  });
  return spots;
}

// ====== Transactions for dashboard ======
function generateTransactions() {
  const txs = [];
  const names = ["ค่าเช่าห้อง 308","ค่าเช่าห้อง 412","ค่าน้ำ-ไฟ ห้อง 205","ค่าจอด B12 (รายเดือน)","ค่าจอด A07 (รายวัน)","ค่าเช่าห้อง 519 (รายวัน)","ค่าเช่าห้อง 121","มัดจำห้อง 110","ค่าจอด C03","ค่าเช่าห้อง 414 (รายวัน)","ค่าน้ำ-ไฟ ห้อง 304","ค่าเช่าห้อง 220"];
  const cats = ["room-month","room-month","room-month","park-month","park-day","room-day","room-month","room-month","park-month","room-day","room-month","room-month"];
  const amts = [4500, 4800, 1280, 1800, 80, 580, 4200, 9000, 1500, 620, 1450, 4500];
  for (let i = 0; i < names.length; i++) {
    txs.push({
      id: `TX${2000+i}`,
      label: names[i],
      cat: cats[i],
      amount: amts[i],
      date: `2026-05-${String(11 - (i % 10)).padStart(2,"0")}`,
      method: ["เงินสด","โอน","QR PromptPay"][i % 3],
    });
  }
  return txs;
}

const PROMOTIONS = [
  { id: "PR01", name: "รายสัปดาห์ลด 15%", type: "daily-week", value: 15, desc: "เหมาจ่าย 7 คืน ลด 15%" },
  { id: "PR02", name: "รายสัปดาห์ลด 20% (ห้องใหญ่)", type: "daily-week", value: 20, desc: "เหมา 7 คืนสำหรับห้องใหญ่" },
  { id: "PR03", name: "ที่จอด 3 เดือน ลด 10%", type: "parking-bulk", value: 10, desc: "ชำระล่วงหน้า 3 เดือน" },
  { id: "PR04", name: "ที่จอด 6 เดือน ลด 18%", type: "parking-bulk", value: 18, desc: "ชำระล่วงหน้า 6 เดือน" },
];

const PARK_DAILY_LOG = [
  { id: "PD1", date: "2026-05-11", plate: "1กข 4427", spot: "A03", hours: 6, amount: 80, method: "เงินสด" },
  { id: "PD2", date: "2026-05-11", plate: "9ขค 2210", spot: "B11", hours: 24, amount: 120, method: "QR PromptPay" },
  { id: "PD3", date: "2026-05-10", plate: "3มย 9981", spot: "A21", hours: 12, amount: 100, method: "เงินสด" },
  { id: "PD4", date: "2026-05-10", plate: "5พล 3340", spot: "C09", hours: 24, amount: 120, method: "โอน" },
  { id: "PD5", date: "2026-05-09", plate: "7รส 1188", spot: "D14", hours: 8, amount: 80, method: "QR PromptPay" },
  { id: "PD6", date: "2026-05-09", plate: "2ภส 5520", spot: "B23", hours: 24, amount: 120, method: "เงินสด" },
];

window.DATA = {
  rooms: generateRooms(),
  parking: generateParking(),
  transactions: generateTransactions(),
  promotions: PROMOTIONS,
  parkDaily: PARK_DAILY_LOG,
};

window.formatBaht = (n) => "฿" + Number(n || 0).toLocaleString("th-TH", { minimumFractionDigits: 0, maximumFractionDigits: 0 });
window.formatBaht2 = (n) => Number(n || 0).toLocaleString("th-TH", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
window.formatDateTH = (s) => {
  if (!s) return "-";
  const d = new Date(s + "T00:00:00");
  return d.toLocaleDateString("th-TH", { day: "numeric", month: "short", year: "2-digit" });
};
