/* ===== data.jsx — icons, shared primitives, seed data ===== */
const { useState, useRef, useEffect } = React;

/* ---- Icons (simple stroke set) ---- */
function Icon({ name, size = 20, stroke = 2 }) {
  const p = {
    width: size, height: size, viewBox: "0 0 24 24", fill: "none",
    stroke: "currentColor", strokeWidth: stroke, strokeLinecap: "round", strokeLinejoin: "round",
  };
  const paths = {
    back: <polyline points="15 18 9 12 15 6" />,
    plus: <g><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></g>,
    logout: <g><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" /><polyline points="16 17 21 12 16 7" /><line x1="21" y1="12" x2="9" y2="12" /></g>,
    layers: <g><polygon points="12 2 2 7 12 12 22 7 12 2" /><polyline points="2 17 12 22 22 17" /><polyline points="2 12 12 17 22 12" /></g>,
    drill: <g><line x1="12" y1="3" x2="12" y2="13" /><path d="M9 13h6l-3 8-3-8z" /><line x1="8" y1="3" x2="16" y2="3" /></g>,
    ruler: <g><path d="M3 9l12-6 6 12-12 6-6-12z" /><path d="M8 7l2 4M12 5l2 4M16 9l2 4" /></g>,
    refresh: <g><polyline points="23 4 23 10 17 10" /><polyline points="1 20 1 14 7 14" /><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15" /></g>,
    droplet: <g><path d="M12 2.69l5.66 5.66a8 8 0 1 1-11.31 0z" /></g>,
    calendar: <g><rect x="3" y="4" width="18" height="18" rx="2" /><line x1="16" y1="2" x2="16" y2="6" /><line x1="8" y1="2" x2="8" y2="6" /><line x1="3" y1="10" x2="21" y2="10" /></g>,
    trash: <g><polyline points="3 6 5 6 21 6" /><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6" /><path d="M10 11v6M14 11v6" /></g>,
    camera: <g><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z" /><circle cx="12" cy="13" r="4" /></g>,
    check: <polyline points="20 6 9 17 4 12" />,
    pin: <g><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /><circle cx="12" cy="10" r="3" /></g>,
    mail: <g><rect x="2" y="4" width="20" height="16" rx="2" /><polyline points="22 6 12 13 2 6" /></g>,
    lock: <g><rect x="3" y="11" width="18" height="11" rx="2" /><path d="M7 11V7a5 5 0 0 1 10 0v4" /></g>,
    chevron: <polyline points="9 18 15 12 9 6" />,
    user: <g><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2" /><circle cx="12" cy="7" r="4" /></g>,
    image: <g><rect x="3" y="3" width="18" height="18" rx="2" /><circle cx="8.5" cy="8.5" r="1.5" /><polyline points="21 15 16 10 5 21" /></g>,
    folder: <path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z" />,
    flask: <g><path d="M9 3h6M10 3v6l-5 9a2 2 0 0 0 1.8 3h10.4a2 2 0 0 0 1.8-3l-5-9V3" /><line x1="7" y1="15" x2="17" y2="15" /></g>,
    activity: <polyline points="22 12 18 12 15 21 9 3 6 12 2 12" />,
    cloud: <path d="M18 10h-1.26A8 8 0 1 0 9 20h9a5 5 0 0 0 0-10z" />,
    home: <g><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" /><polyline points="9 22 9 12 15 12 15 22" /></g>,
    search: <g><circle cx="11" cy="11" r="8" /><line x1="21" y1="21" x2="16.65" y2="16.65" /></g>,
    edit: <g><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" /><path d="M18.5 2.5a2.12 2.12 0 0 1 3 3L12 15l-4 1 1-4z" /></g>,
    archive: <g><polyline points="21 8 21 21 3 21 3 8" /><rect x="1" y="3" width="22" height="5" /><line x1="10" y1="12" x2="14" y2="12" /></g>,
    download: <g><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><polyline points="7 10 12 15 17 10" /><line x1="12" y1="15" x2="12" y2="3" /></g>,
    sun: <g><circle cx="12" cy="12" r="5" /><line x1="12" y1="1" x2="12" y2="3" /><line x1="12" y1="21" x2="12" y2="23" /><line x1="4.2" y1="4.2" x2="5.6" y2="5.6" /><line x1="18.4" y1="18.4" x2="19.8" y2="19.8" /><line x1="1" y1="12" x2="3" y2="12" /><line x1="21" y1="12" x2="23" y2="12" /><line x1="4.2" y1="19.8" x2="5.6" y2="18.4" /><line x1="18.4" y1="5.6" x2="19.8" y2="4.2" /></g>,
    moon: <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />,
    x: <g><line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" /></g>,
    filter: <polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3" />,
    sliders: <g><line x1="4" y1="21" x2="4" y2="14" /><line x1="4" y1="10" x2="4" y2="3" /><line x1="12" y1="21" x2="12" y2="12" /><line x1="12" y1="8" x2="12" y2="3" /><line x1="20" y1="21" x2="20" y2="16" /><line x1="20" y1="12" x2="20" y2="3" /><line x1="1" y1="14" x2="7" y2="14" /><line x1="9" y1="8" x2="15" y2="8" /><line x1="17" y1="16" x2="23" y2="16" /></g>,
    lockopen: <g><rect x="3" y="11" width="18" height="11" rx="2" /><path d="M7 11V7a5 5 0 0 1 9.9-1" /></g>,
    arrowright: <g><line x1="5" y1="12" x2="19" y2="12" /><polyline points="12 5 19 12 12 19" /></g>,
    table: <g><rect x="3" y="3" width="18" height="18" rx="2" /><line x1="3" y1="9" x2="21" y2="9" /><line x1="3" y1="15" x2="21" y2="15" /><line x1="12" y1="3" x2="12" y2="21" /></g>,
    eye: <g><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" /><circle cx="12" cy="12" r="3" /></g>,
    bell: <g><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.7 21a2 2 0 0 1-3.4 0" /></g>,
    map: <g><polygon points="1 6 1 22 8 18 16 22 23 18 23 2 16 6 8 2 1 6" /><line x1="8" y1="2" x2="8" y2="18" /><line x1="16" y1="6" x2="16" y2="22" /></g>,
    gps: <g><circle cx="12" cy="12" r="3" /><circle cx="12" cy="12" r="8" /><line x1="12" y1="1" x2="12" y2="4" /><line x1="12" y1="20" x2="12" y2="23" /><line x1="1" y1="12" x2="4" y2="12" /><line x1="20" y1="12" x2="23" y2="12" /></g>,
    target: <g><circle cx="12" cy="12" r="9" /><line x1="12" y1="1" x2="12" y2="5" /><line x1="12" y1="19" x2="12" y2="23" /><line x1="1" y1="12" x2="5" y2="12" /><line x1="19" y1="12" x2="23" y2="12" /><circle cx="12" cy="12" r="2.4" fill="currentColor" stroke="none" /></g>,
    square: <rect x="4" y="5" width="16" height="14" rx="1.5" />,
    polygon: <polygon points="12 3 21 8.5 18.5 20 5.5 20 3 8.5" />,
    save: <g><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z" /><polyline points="17 21 17 13 7 13 7 21" /><polyline points="7 3 7 8 15 8" /></g>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
}

/* ---- Status badge ---- */
function StatusBadge({ status }) {
  const map = {
    "Active": "green", "Complete": "green",
    "In progress": "amber",
    "Closed": "slate", "On hold": "slate",
  };
  const et = { "Active": "Aktiivne", "Complete": "Valmis", "In progress": "Töös", "Closed": "Suletud", "On hold": "Ootel" };
  return (
    <span className={"badge " + (map[status] || "slate")}>
      <span className="dot" />{et[status] || status}
    </span>
  );
}

const SAMPLE_TYPES = [
  ["K", "K — Kott"],
  ["N", "N — Niiskus"],
  ["V", "V — Vesi"],
  ["M", "M — Monoliit"],
];

/* Borehole / investigation point types (field app). Code = ID prefix:
   a hole's number carries its type, e.g. a Käsipuurauk → KP1, KP2…
   Order matters for prefix matching — longest codes first so "PA-LP"
   is tested before "PA". */
const BOREHOLE_TYPES = [
  ["PA", "Puurauk"],
  ["PA-LP", "Puurauk – Löökpenetratsioon"],
  ["KP", "Käsipuurauk"],
  ["SLP", "Suru-löökpenetratsioon"],
  ["SP", "Surupenetratsioon"],
  ["TK", "Tiivikkatse"],
];
const BOREHOLE_TYPE_CODES = BOREHOLE_TYPES.map(function (t) { return t[0]; })
  .sort(function (a, b) { return b.length - a.length; }); // longest-first
function boreholeTypeLabel(code) {
  var t = BOREHOLE_TYPES.find(function (x) { return x[0] === code; });
  return t ? t[1] : code;
}

/* ================= SEED DATA (first run only) =================
   Estonian road-investigation projects. `route` = polyline the
   seeded boreholes scatter along (WGS84); `progress` = stage
   distribution [Välitööd..Aruanne]. After first run all data
   lives in IndexedDB (store.js) and these are ignored.          */
const RP_SEED = {
  projects: [
    {
      id: "p1", name: "Põhimaantee 2 (E263) Kose–Võõbu", code: "GL26021", short: "Kose–Võõbu",
      client: "Transpordiamet", status: "Active",
      engineer: "A. Vaher", contractor: "Geopartner OÜ", lead: "M. Tamm",
      location: "Harjumaa, Kose vald", start: "2026-04-12", method: "Cable percussion + Rotary",
      progress: [2, 3, 4, 3, 4, 2],
      route: [[59.186, 25.166], [59.155, 25.235], [59.118, 25.310], [59.082, 25.388], [59.043, 25.452]],
    },
    {
      id: "p2", name: "Põhimaantee 4 (E67) Pärnu–Uulu", code: "GL26007", short: "Pärnu–Uulu",
      client: "Transpordiamet", status: "Active",
      engineer: "R. Saar", contractor: "Geotest OÜ", lead: "M. Tamm",
      location: "Pärnumaa", start: "2026-03-02", method: "Rotary",
      progress: [8, 10, 6, 5, 3, 0],
      route: [[58.371, 24.521], [58.345, 24.545], [58.318, 24.568], [58.292, 24.588], [58.262, 24.607]],
    },
    {
      id: "p3", name: "Tugimaantee 92 Tartu–Viljandi km 12–19", code: "GL26035", short: "Tartu–Viljandi_t92",
      client: "Transpordiamet", status: "Active",
      engineer: "K. Lepik", contractor: "Geopartner OÜ", lead: "J. Kask",
      location: "Tartumaa", start: "2026-05-28", method: "Cable percussion",
      progress: [11, 0, 0, 0, 0, 0],
      route: [[58.355, 26.527], [58.345, 26.470], [58.333, 26.412], [58.320, 26.355]],
    },
    {
      id: "p4", name: "Kõrvalmaantee 11401 Jüri–Vaida kergliiklustee", code: "GL26019", short: "Jüri–Vaida",
      client: "Rae Vallavalitsus", status: "On hold",
      engineer: "R. Saar", contractor: "PuurTehnika OÜ", lead: "J. Kask",
      location: "Harjumaa, Rae vald", start: "2026-05-04", method: "Trial pit",
      progress: [4, 6, 6, 4, 2, 2],
      route: [[59.364, 24.943], [59.352, 24.982], [59.339, 25.021], [59.326, 25.058]],
    },
    {
      id: "p5", name: "Riigitee 11 Tallinna ringtee, Luige–Saku", code: "GL25067", short: "Luige–Saku",
      client: "Transpordiamet", status: "Closed",
      engineer: "A. Vaher", contractor: "Geotest OÜ", lead: "M. Tamm",
      location: "Harjumaa, Saku vald", start: "2026-02-10", method: "Cable percussion",
      progress: [0, 0, 0, 0, 0, 9],
      route: [[59.342, 24.713], [59.330, 24.680], [59.318, 24.648], [59.305, 24.617]],
    },
  ],

  /* Detailed seed log — BH101 (Kose–Võõbu road structure + native soils).
     east/north are filled from the seeded position at first run. */
  logs: {
    BH101: {
      loca: {
        id: "BH101", type: "BH",
        east: "", north: "",
        depth: "24.50", veetase: "2.40", veeStrike: "3.10", start: "2026-05-18", end: "2026-05-20", gpsAcc: 4, gpsDist: 2,
        engineer: "A. Vaher", contractor: "Geopartner OÜ",
        method: "Light cable percussion",
      },
      geos: [
        { top: "0.00", base: "0.06", fieldMat: "f_asfalt", mat: "m5", desc: "Asfaltbetoon, kulumiskiht (ACsurf)." },
        { top: "0.06", base: "0.21", fieldMat: "f_asfalt", mat: "m8", desc: "Asfaltbetoon, kandev kiht (ACbase)." },
        { top: "0.21", base: "0.58", fieldMat: "f_killustik", mat: "m33", desc: "Purustatud pae-/kruuskillustik, kandev alus." },
        { top: "0.58", base: "1.30", fieldMat: "f_kruus", mat: "m41", desc: "Kruusliiv ja jämeliiv, keskmise tihedusega, pruun." },
        { top: "1.30", base: "4.00", fieldMat: "f_liiv", mat: "m43", desc: "Keskliiv, kollakaspruun, märg alates 2.4 m." },
        { top: "4.00", base: "24.50", fieldMat: "f_liivsavi", mat: "m51", desc: "Raske liivsavi/savi, sinakashall, sileda konsistentsiga." },
      ],
      samp: [
        { top: "1.00", base: "1.45", ref: "U1", type: "U" },
        { top: "2.50", base: "2.50", ref: "B1", type: "B" },
        { top: "4.00", base: "4.45", ref: "U2", type: "U" },
        { top: "8.00", base: "8.00", ref: "D3", type: "D" },
      ],
      ispt: [
        { depth: "1.50", n: "12", ref: "SPT1" },
        { depth: "3.00", n: "24", ref: "SPT2" },
        { depth: "6.00", n: "31", ref: "SPT3" },
        { depth: "9.00", n: "38", ref: "SPT4" },
      ],
      photos: ["BH101_001.jpg", "BH101_002.jpg", "BH101_arising.jpg"],
    },
  },
};

/* ---- live-data accessors (store-backed after RPStore.init) ---- */
function blankLoca(id) {
  return { id: id || "", type: "PA", east: "", north: "", depth: "", veetase: "", veeStrike: "", start: "", end: "", engineer: "A. Vaher", contractor: "Geopartner OÜ", method: "Light cable percussion" };
}
function logFor(bhId) {
  const l = window.RPStore && RPStore.ready ? RPStore.getLog(bhId) : null;
  if (l) return l;
  return { loca: blankLoca(bhId), geos: [], samp: [], ispt: [], photos: [] };
}

/* Defaults until the store hydrates (replaced by store.js refreshGlobals) */
window.PROJECTS = window.PROJECTS || [];
window.BOREHOLES = window.BOREHOLES || {};
window.BOREHOLE_ITEMS = window.BOREHOLE_ITEMS || [];
window.SAMPLE_LOG = window.SAMPLE_LOG || {};
window.PROJECT_META = window.PROJECT_META || {};

/* ---- Reaalprojekt brand logo (transparent; inverts to white in dark mode) ---- */
function RPLogo({ height = 22 }) {
  return (
    <span className="rplogo">
      <img src="assets/reaalprojekt-logo.png" alt="Reaalprojekt" style={{ height }} />
    </span>
  );
}

Object.assign(window, {
  Icon, StatusBadge, SAMPLE_TYPES, BOREHOLE_TYPES, BOREHOLE_TYPE_CODES, boreholeTypeLabel, RP_SEED,
  blankLoca, logFor, useState, useRef, useEffect, RPLogo,
});
