// data.jsx — mock data + icon set for AeroLog (aircraft maintenance line log)
// All exported to window at the end.

/* ----------------------------- ICONS ----------------------------- */
// Stroke-based technical glyphs. Inherit currentColor.
const Ic = {
  today: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="3" y="5" width="18" height="16" rx="2" stroke="currentColor" strokeWidth="1.8"/><path d="M3 9h18M8 3v4M16 3v4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/><circle cx="12" cy="14" r="2" fill="currentColor"/></svg>),
  logs: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M5 4h11l3 3v13a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M8 10h8M8 14h8M8 18h5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  flight: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M21 16.5 13.5 12V5.5a1.5 1.5 0 0 0-3 0V12L3 16.5V18l7.5-2v3.5L8.5 21v1l3.5-1 3.5 1v-1l-2-1.5V16l7.5 2v-1.5Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/></svg>),
  cb: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="12" r="8.5" stroke="currentColor" strokeWidth="1.8"/><path d="M12 7v5l3 2" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  user: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="8" r="3.5" stroke="currentColor" strokeWidth="1.8"/><path d="M5 20c0-3.6 3.1-6 7-6s7 2.4 7 6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  plus: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M12 5v14M5 12h14" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round"/></svg>),
  search: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="11" cy="11" r="6.5" stroke="currentColor" strokeWidth="1.8"/><path d="m16 16 4 4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  back: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="m15 5-7 7 7 7" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  chevron: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="m9 6 6 6-6 6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  check: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="m5 13 4 4L19 7" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  close: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M6 6l12 12M18 6 6 18" stroke="currentColor" strokeWidth="2" strokeLinecap="round"/></svg>),
  camera: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M4 8h3l1.5-2h7L17 8h3a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><circle cx="12" cy="13" r="3.2" stroke="currentColor" strokeWidth="1.8"/></svg>),
  part: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M12 3 4 7.5v9L12 21l8-4.5v-9L12 3Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M4 7.5 12 12l8-4.5M12 12v9" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/></svg>),
  tool: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M14.5 6a3.5 3.5 0 0 0-4.6 4.3L4 16.2 6.8 19l5.9-5.9A3.5 3.5 0 0 0 17 8.5l-2.2 2.2-1.8-.4-.4-1.8L14.8 6.3Z" stroke="currentColor" strokeWidth="1.7" strokeLinejoin="round"/></svg>),
  fuel: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="4" y="4" width="9" height="16" rx="1.5" stroke="currentColor" strokeWidth="1.8"/><path d="M4 10h9M16 8l3 3v7a1.5 1.5 0 0 1-3 0V7" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  clock: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="12" r="8.5" stroke="currentColor" strokeWidth="1.8"/><path d="M12 7.5V12l3 2" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  warn: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M12 4 2.5 20h19L12 4Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M12 10v4.5M12 17.2v.1" stroke="currentColor" strokeWidth="2" strokeLinecap="round"/></svg>),
  doc: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M6 3h8l4 4v14a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M13 3v5h5" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/></svg>),
  pin: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M12 21s7-5.6 7-11a7 7 0 1 0-14 0c0 5.4 7 11 7 11Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><circle cx="12" cy="10" r="2.5" stroke="currentColor" strokeWidth="1.8"/></svg>),
  sync: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M4 12a8 8 0 0 1 13.7-5.6L20 8M20 12a8 8 0 0 1-13.7 5.6L4 16" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/><path d="M20 4v4h-4M4 20v-4h4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  bolt: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M13 3 4 14h6l-1 7 9-11h-6l1-7Z" stroke="currentColor" strokeWidth="1.7" strokeLinejoin="round"/></svg>),
  star: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="m12 4 2.4 4.9 5.4.8-3.9 3.8.9 5.4-4.8-2.5-4.8 2.5.9-5.4L4 9.7l5.4-.8L12 4Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/></svg>),
  filter: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M4 6h16M7 12h10M10 18h4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  grid: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="4" y="4" width="7" height="7" rx="1.4" stroke="currentColor" strokeWidth="1.8"/><rect x="13" y="4" width="7" height="7" rx="1.4" stroke="currentColor" strokeWidth="1.8"/><rect x="4" y="13" width="7" height="7" rx="1.4" stroke="currentColor" strokeWidth="1.8"/><rect x="13" y="13" width="7" height="7" rx="1.4" stroke="currentColor" strokeWidth="1.8"/></svg>),
  mic: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="9" y="3" width="6" height="11" rx="3" stroke="currentColor" strokeWidth="1.8"/><path d="M5 11a7 7 0 0 0 14 0M12 18v3" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  arrowR: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  tow: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M3 16V8h9v8M12 11h3.5l3.5 3v2h-3M3 16h2M9 16h6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/><circle cx="7" cy="18" r="1.9" stroke="currentColor" strokeWidth="1.8"/><circle cx="17" cy="18" r="1.9" stroke="currentColor" strokeWidth="1.8"/></svg>),
  dent: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="3" y="6" width="18" height="12" rx="2" stroke="currentColor" strokeWidth="1.8"/><path d="M6 12c1.4-2.3 3-2.3 4.2 0 1.2 2.3 2.8 2.3 4.2 0 1-1.7 2.2-2 3.6-1" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  ext: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M14 5h5v5M19 5l-8 8M18 13v5a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  wx: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M7 18h9a3.5 3.5 0 0 0 .5-6.96A5 5 0 0 0 7 9.6 3.2 3.2 0 0 0 7 18Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M9 21l-1 1.5M13 21l-1 1.5M17 21l-1 1.5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  swap: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M16 4l3.5 3.5L16 11M19 7.5H7M8 13l-3.5 3.5L8 20M5 16.5h12" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  bell: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M6 9.5a6 6 0 0 1 12 0c0 4.5 2 5.5 2 5.5H4s2-1 2-5.5Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M9.5 19a2.5 2.5 0 0 0 5 0" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
  edit: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M4 20l1.2-4.2L15.3 5.7a2 2 0 0 1 2.8 0l.2.2a2 2 0 0 1 0 2.8L8.2 18.8 4 20Z" stroke="currentColor" strokeWidth="1.8" strokeLinejoin="round"/><path d="M13.5 7.5l3 3" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>),
};

// Dent & Buckle records live in the user's Google Drive — the D&B tile opens this.
// (u/1 = 2nd signed-in Google account; change the index if it opens the wrong one.)
const DNB_URL = "https://drive.google.com/drive/u/1/search?q=dent%20%26%20buckle";

// Live flight tracking — opens FlightStats for a specific flight on today's date.
// Their internal flightId isn't needed; carrier + number + date resolves the flight.
// (Switch TRACK_BASE to FlightAware/FR24 here if preferred.)
function trackUrl(flightId) {
  const m = String(flightId || "").match(/^([A-Za-z]*)\D*(\d+)/);
  const carrier = (m && m[1]) ? m[1].toUpperCase() : "FD";   // FD = Thai AirAsia
  const num = m ? m[2] : "";
  const d = new Date();
  const y = d.getFullYear(), mo = String(d.getMonth() + 1).padStart(2, "0"), da = String(d.getDate()).padStart(2, "0");
  if (!num) return "https://www.flightstats.com/v2/";
  return `https://www.flightstats.com/v2/flight-tracker/${carrier}/${num}?year=${y}&month=${mo}&date=${da}`;
}

/* ----------------------------- ATA CHAPTERS ----------------------------- */
const ATA = [
  { c: "21", t: "Air Conditioning", th: "ปรับอากาศ" },
  { c: "22", t: "Auto Flight", th: "ระบบบินอัตโนมัติ" },
  { c: "23", t: "Communications", th: "สื่อสาร" },
  { c: "24", t: "Electrical Power", th: "ไฟฟ้า" },
  { c: "25", t: "Equipment / Furnishings", th: "อุปกรณ์ภายใน" },
  { c: "26", t: "Fire Protection", th: "ป้องกันอัคคีภัย" },
  { c: "27", t: "Flight Controls", th: "ควบคุมการบิน" },
  { c: "28", t: "Fuel", th: "เชื้อเพลิง" },
  { c: "29", t: "Hydraulic Power", th: "ไฮดรอลิก" },
  { c: "30", t: "Ice & Rain Protection", th: "กันน้ำแข็ง/ฝน" },
  { c: "31", t: "Indicating / Recording", th: "แสดงผล/บันทึก" },
  { c: "32", t: "Landing Gear", th: "ฐานล้อ" },
  { c: "33", t: "Lights", th: "ไฟส่องสว่าง" },
  { c: "34", t: "Navigation", th: "นำร่อง" },
  { c: "35", t: "Oxygen", th: "ออกซิเจน" },
  { c: "36", t: "Pneumatic", th: "ลมอัด" },
  { c: "38", t: "Water / Waste", th: "น้ำ/ของเสีย" },
  { c: "49", t: "APU", th: "เครื่องกำเนิดไฟเสริม" },
  { c: "52", t: "Doors", th: "ประตู" },
  { c: "53", t: "Fuselage", th: "ลำตัว" },
  { c: "57", t: "Wings", th: "ปีก" },
  { c: "71", t: "Power Plant", th: "ชุดเครื่องยนต์" },
  { c: "72", t: "Engine", th: "เครื่องยนต์" },
  { c: "73", t: "Engine Fuel & Control", th: "เชื้อเพลิงเครื่องยนต์" },
  { c: "78", t: "Exhaust", th: "ไอเสีย" },
  { c: "79", t: "Oil", th: "น้ำมันหล่อลื่น" },
  { c: "80", t: "Starting", th: "ระบบสตาร์ท" },
];
const ATA_RECENT = ["28", "24", "32", "27"];
const ATA_FAV = ["28", "32"];

/* ----------------------------- FLEET + FLIGHTS ----------------------------- */
// Empty by default — real fleet/flights are loaded live from the taaplus OPS API
// (api.jsx fetchFromOps → window.FLEET/FLIGHTS). No mock aircraft/flights shown.
const FLEET = [];
const FLIGHTS = [];

/* ----------------------------- PARTS / TOOLS / CB ----------------------------- */
// PART_LIB = optional P/N autocomplete suggestions for the parts editor (empty = none).
const PART_LIB = [];
// Empty fallback — the real Tool Inventory (2,959 tools) loads from api/tools.json
// into window.TOOL_LIB at startup (api.jsx fetchTools).
const TOOL_LIB = [];
// C/B reset guide — keyed by the ECAM fault that triggers it.
// NOTE: ตัวอย่าง — panel / ECAM text / caution ต้องใส่จาก TSM + นโยบาย C/B reset จริงของหน่วยงาน
const CB_LIB = [
  { id: "P6-5", name: "FUEL PUMP LH", panel: "P6", loc: "C/B panel 49VU (overhead rear)", ata: "28",
    ecam: "FUEL L TK PUMP LO PR", note: "reset ได้ 1 ครั้ง · ถ้า trip ซ้ำห้าม reset" },
  { id: "P6-6", name: "FUEL PUMP RH", panel: "P6", loc: "C/B panel 49VU (overhead rear)", ata: "28",
    ecam: "FUEL R TK PUMP LO PR", note: "reset ได้ 1 ครั้ง · ถ้า trip ซ้ำห้าม reset" },
  { id: "P18-12", name: "FADEC ENG 1", panel: "P18", loc: "C/B panel 121VU", ata: "73",
    ecam: "ENG 1 FADEC FAULT", note: "ภาคพื้นเท่านั้น · ทำตามขั้นตอน TSM ก่อน" },
  { id: "P5-3", name: "WINDOW HEAT L", panel: "P5", loc: "C/B panel 49VU", ata: "30",
    ecam: "ANTI ICE L WINDSHIELD", note: "ปล่อยให้เย็นก่อน reset" },
  { id: "P11-7", name: "GALLEY 2", panel: "P11", loc: "C/B panel 105VU (aft galley)", ata: "25",
    ecam: "GALLEY 2 FAULT", note: "ตัดโหลด galley ก่อน reset" },
];

/* ----------------------------- LOGS / AC INFO / MR2 ----------------------------- */
// Work Logs start empty (saved to localStorage as the tech creates them).
const SEED_LOGS = [];
// AC_INFO (MSN/SELCAL/W&B/autoland/fumigation) + MR2 (deferred defects) are loaded
// live from the Google Sheet via api.jsx → window.AC_INFO / window.MR2. Empty = no mock.
const AC_INFO = {};
const MR2 = {};

/* ----------------------------- AIRPORTS (city name lookup) ----------------------------- */
// Thai AirAsia (DMK) domestic + common intl — for the flight-status card city names.
const AIRPORTS = {
  DMK: "Bangkok", BKK: "Bangkok", CNX: "Chiang Mai", CEI: "Chiang Rai", HKT: "Phuket",
  KBV: "Krabi", USM: "Koh Samui", HDY: "Hat Yai", URT: "Surat Thani", NST: "Nakhon Si Thammarat",
  TST: "Trang", UNN: "Ranong", CJM: "Chumphon", UTP: "Pattaya/U-Tapao",
  UTH: "Udon Thani", UBP: "Ubon Ratchathani", KKC: "Khon Kaen", SNO: "Sakon Nakhon",
  KOP: "Nakhon Phanom", ROI: "Roi Et", BFV: "Buriram", NAW: "Narathiwat", LOE: "Loei",
  PHS: "Phitsanulok", THS: "Sukhothai", NNT: "Nan", HGN: "Mae Hong Son", MAQ: "Mae Sot", PRH: "Phrae",
  SIN: "Singapore", KUL: "Kuala Lumpur", RGN: "Yangon", SGN: "Ho Chi Minh", HAN: "Hanoi",
  HKG: "Hong Kong", MFM: "Macau", CMB: "Colombo", DAC: "Dhaka", CCU: "Kolkata", GAY: "Gaya",
};
const airportName = (c) => AIRPORTS[c] || c || "—";

/* ----------------------------- SHARED HELPERS ----------------------------- */
// photos field is an array of image data-URLs on new logs, but seed/legacy logs
// store a plain count — these read either shape safely.
const photoArr = (p) => (Array.isArray(p) ? p : []);
const photoCount = (p) => (Array.isArray(p) ? p.length : (typeof p === "number" ? p : 0));
// "HH:MM" now (local) for stamping freshly created logs.
const nowHM = () => { const n = new Date(); return String(n.getHours()).padStart(2, "0") + ":" + String(n.getMinutes()).padStart(2, "0"); };

// Arrival info for a flight: minutes-to-arrival (local), landed flag — for the
// transit arrival alerts / watch list.
function flightArrival(f) {
  const landed = !!(f && f.arrAct) || /Land/i.test((f && f.opStatus) || "");
  const arr = (f && (f.arrAct || f.arrEst || f.arrSched)) || "";
  const m = /(\d{1,2}):(\d{2})/.exec(arr);
  let mins = null;
  if (m) { const aM = (+m[1]) * 60 + (+m[2]); const d = new Date(); mins = aM - (d.getHours() * 60 + d.getMinutes()); }
  return { arr, mins, landed };
}

/* ----------------------------- SHIFT ROSTER ----------------------------- */
// 4-on / 4-off pattern: 2 morning, then 2 night, then 4 days off = 8-day cycle.
// Anchored to Sat 06-Jun-2026 = cycle day 0 (matches the user's Google Calendar
// roster: D Shift 1/2 = morning ☀, D Shift 3/4 = night). Adjust SHIFT_HOURS /
// SHIFT_ANCHOR if the real start day or shift times differ.
const SHIFT_ANCHOR = Date.UTC(2026, 5, 6);
const SHIFT_HOURS = { morning: "08:00–20:00", night: "20:00–08:00" };  // 12h shifts
const SHIFT_CYCLE = [
  { kind: "morning", n: 1 }, { kind: "morning", n: 2 },
  { kind: "night", n: 3 }, { kind: "night", n: 4 },
  { kind: "off" }, { kind: "off" }, { kind: "off" }, { kind: "off" },
];
const SHIFT_LABEL = { morning: "Morning shift", night: "Night shift", off: "Day off" };
// reasons for a manual override (call-in / OT / shift swap / leave)
const SHIFT_REASON = { callin: "Call-in", ot: "OT", swap: "Swap", leave: "Leave" };
const shiftISO = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;

// Base roster (the fixed 4-on/4-off pattern) for a date.
function shiftInfo(date) {
  const d = date || new Date();
  const day = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
  let i = Math.floor((day - SHIFT_ANCHOR) / 86400000) % 8;
  if (i < 0) i += 8;
  const c = SHIFT_CYCLE[i];
  return { kind: c.kind, n: c.n, idx: i, label: SHIFT_LABEL[c.kind], hours: SHIFT_HOURS[c.kind] || "", working: c.kind !== "off" };
}
// Effective roster = base pattern with any manual override applied (overrides is a
// map keyed by ISO date → { kind, reason, hours, note }).
function effectiveShift(date, overrides) {
  const d = date || new Date();
  const base = shiftInfo(d);
  const o = overrides && overrides[shiftISO(d)];
  if (!o) return { ...base, override: null };
  const kind = o.kind || base.kind;
  return {
    kind, idx: base.idx, n: base.n, label: SHIFT_LABEL[kind],
    hours: o.hours || SHIFT_HOURS[kind] || "", working: kind !== "off", override: o,
  };
}
// Next working shift after `date` (used on off days to show "back on …").
function shiftNext(date, overrides) {
  const base = date || new Date();
  for (let k = 1; k <= 10; k++) {
    const d = new Date(base); d.setDate(base.getDate() + k);
    const s = effectiveShift(d, overrides);
    if (s.working) return { ...s, in: k, date: d };
  }
  return null;
}

Object.assign(window, {
  Ic, ATA, ATA_RECENT, ATA_FAV, FLEET, FLIGHTS,
  PART_LIB, TOOL_LIB, CB_LIB, SEED_LOGS, AC_INFO, MR2,
  photoArr, photoCount, nowHM, flightArrival, shiftInfo, shiftNext, effectiveShift, shiftISO, SHIFT_HOURS, SHIFT_LABEL, SHIFT_REASON,
  DNB_URL, trackUrl, AIRPORTS, airportName,
});
