/* =========================================================
   NETRA AI — icon set (inline SVG) & small widgets
   ========================================================= */

const Icon = ({ name, size = 16, ...rest }) => {
  const s = { width: size, height: size, ...(rest.style || {}) };
  const common = { fill: "none", stroke: "currentColor", strokeWidth: 1.6, strokeLinecap: "round", strokeLinejoin: "round" };
  const paths = {
    eye:        <><circle cx="12" cy="12" r="3" /><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12Z" /></>,
    live:       <><rect x="3" y="6" width="14" height="12" rx="2" /><path d="M17 10l4-2v8l-4-2Z" /><circle cx="6.5" cy="9" r="0.8" fill="currentColor" stroke="none" /></>,
    alert:      <><path d="M12 2 2 20h20Z" /><path d="M12 10v4" /><circle cx="12" cy="17" r="0.8" fill="currentColor" stroke="none" /></>,
    rules:      <><path d="M4 6h7" /><circle cx="15" cy="6" r="2" /><path d="M4 12h11" /><circle cx="19" cy="12" r="2" /><path d="M4 18h7" /><circle cx="15" cy="18" r="2" /></>,
    map:        <><path d="M3 6l6-2 6 2 6-2v14l-6 2-6-2-6 2Z" /><path d="M9 4v16" /><path d="M15 6v16" /></>,
    chart:      <><path d="M4 20V4" /><path d="M4 20h16" /><path d="M8 16l3-5 4 2 5-7" /></>,
    devices:    <><rect x="3" y="4" width="18" height="12" rx="2" /><path d="M8 20h8M12 16v4" /></>,
    users:      <><circle cx="9" cy="9" r="3.5" /><path d="M2 20c1-3.5 4-5.5 7-5.5s6 2 7 5.5" /><circle cx="17" cy="8" r="2.5" /><path d="M22 18c-.5-2.5-2-3.5-4-3.7" /></>,
    incident:   <><path d="M12 3l9 4-1.5 9L12 21l-7.5-5L3 7Z" /><path d="M9 11l2 2 4-4" /></>,
    settings:   <><circle cx="12" cy="12" r="3" /><path d="M19 12a7 7 0 0 0-.1-1.2l2-1.6-2-3.4-2.3.9a7 7 0 0 0-2.1-1.2L14 3h-4l-.5 2.5a7 7 0 0 0-2.1 1.2l-2.3-.9-2 3.4 2 1.6A7 7 0 0 0 5 12c0 .4 0 .8.1 1.2l-2 1.6 2 3.4 2.3-.9a7 7 0 0 0 2.1 1.2L10 21h4l.5-2.5a7 7 0 0 0 2.1-1.2l2.3.9 2-3.4-2-1.6c.1-.4.1-.8.1-1.2Z" /></>,
    search:     <><circle cx="11" cy="11" r="7" /><path d="m20 20-3.5-3.5" /></>,
    bell:       <><path d="M6 10a6 6 0 0 1 12 0v4l1.5 3h-15L6 14Z" /><path d="M10 20a2 2 0 0 0 4 0" /></>,
    plus:       <><path d="M12 5v14M5 12h14" /></>,
    filter:     <><path d="M3 5h18l-7 9v6l-4-2v-4Z" /></>,
    download:   <><path d="M12 4v12m0 0-4-4m4 4 4-4M4 20h16" /></>,
    play:       <><path d="M7 5v14l12-7Z" fill="currentColor" stroke="none" /></>,
    pause:      <><rect x="6" y="5" width="4" height="14" fill="currentColor" stroke="none" /><rect x="14" y="5" width="4" height="14" fill="currentColor" stroke="none" /></>,
    rewind:     <><path d="M11 6 4 12l7 6Z" fill="currentColor" stroke="none" /><path d="M20 6l-7 6 7 6Z" fill="currentColor" stroke="none" /></>,
    ff:         <><path d="M13 6l7 6-7 6Z" fill="currentColor" stroke="none" /><path d="M4 6l7 6-7 6Z" fill="currentColor" stroke="none" /></>,
    maximize:   <><path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5" /></>,
    mic:        <><rect x="9" y="3" width="6" height="11" rx="3" /><path d="M5 11a7 7 0 0 0 14 0M12 18v3" /></>,
    cam:        <><rect x="3" y="6" width="14" height="12" rx="2" /><path d="M17 10l4-2v8l-4-2Z" /></>,
    sensor:     <><circle cx="12" cy="12" r="2" /><path d="M8 12a4 4 0 0 1 8 0M5 12a7 7 0 0 1 14 0" /></>,
    door:       <><path d="M6 3h12v18H6Z" /><circle cx="14.5" cy="12" r="0.9" fill="currentColor" stroke="none" /></>,
    chevron:    <><path d="m9 6 6 6-6 6" /></>,
    close:      <><path d="M6 6l12 12M18 6 6 18" /></>,
    check:      <><path d="m5 12 5 5 9-11" /></>,
    flag:       <><path d="M5 21V4m0 0h10l-2 4 2 4H5" /></>,
    expand:     <><path d="M3 9V3h6M21 9V3h-6M3 15v6h6M21 15v6h-6" /></>,
    moon:       <><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" /></>,
    sun:        <><circle cx="12" cy="12" r="4" /><path d="M12 2v2M12 20v2M2 12h2M20 12h2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" /></>,
    bolt:       <><path d="M13 2 4 14h7l-2 8 9-12h-7Z" /></>,
    history:    <><path d="M3 12a9 9 0 1 0 3-6.7L3 8" /><path d="M3 3v5h5" /><path d="M12 8v4l3 2" /></>,
  };
  return <svg viewBox="0 0 24 24" style={s} {...common}>{paths[name] || null}</svg>;
};

/* ============ Sparkline ============ */
const Sparkline = ({ data, w = 110, h = 32, color = "var(--iris)", fill = true }) => {
  const max = Math.max(...data, 1), min = Math.min(...data, 0);
  const span = Math.max(max - min, 1);
  const step = w / (data.length - 1);
  const pts = data.map((v, i) => [i * step, h - ((v - min) / span) * (h - 4) - 2]);
  const d = pts.map((p, i) => (i ? "L" : "M") + p[0].toFixed(1) + " " + p[1].toFixed(1)).join(" ");
  const area = d + ` L ${w} ${h} L 0 ${h} Z`;
  const gid = "spk-" + Math.random().toString(36).slice(2, 7);
  return (
    <svg width={w} height={h} style={{ display: "block" }}>
      <defs>
        <linearGradient id={gid} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%"  stopColor={color} stopOpacity="0.35" />
          <stop offset="100%" stopColor={color} stopOpacity="0" />
        </linearGradient>
      </defs>
      {fill && <path d={area} fill={`url(#${gid})`} />}
      <path d={d} fill="none" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
};

/* ============ Iris logo / Splash animation ============ */
const IrisMark = ({ size = 30, animated = false }) => {
  const cls = animated ? "iris-svg" : "";
  return (
    <svg className={cls} viewBox="0 0 100 100" width={size} height={size}>
      <defs>
        <radialGradient id="iris-grad" cx="50%" cy="50%" r="50%">
          <stop offset="0%"  stopColor="#06080d" />
          <stop offset="55%" stopColor="#0a1828" />
          <stop offset="100%" stopColor="#0a1320" />
        </radialGradient>
        <linearGradient id="iris-ring" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%"  stopColor="#4dd6ff" />
          <stop offset="100%" stopColor="#f0a73a" />
        </linearGradient>
      </defs>
      {/* outer almond */}
      <path d="M5 50 Q 50 5 95 50 Q 50 95 5 50 Z" fill="none" stroke="url(#iris-ring)" strokeWidth="2" opacity="0.9" />
      {/* outer rotating ring */}
      <g className="ring-outer">
        <circle cx="50" cy="50" r="36" fill="none" stroke="#4dd6ff" strokeOpacity="0.25" strokeWidth="0.8" />
        <circle cx="50" cy="50" r="36" fill="none" stroke="#4dd6ff" strokeWidth="1.2"
          strokeDasharray="2 8" pathLength="100" />
      </g>
      {/* inner rotating ring */}
      <g className="ring-inner">
        <circle cx="50" cy="50" r="28" fill="none" stroke="#f0a73a" strokeOpacity="0.4" strokeWidth="0.6" />
        <circle cx="50" cy="50" r="28" fill="none" stroke="#f0a73a" strokeWidth="0.6"
          strokeDasharray="1 5" />
      </g>
      {/* iris radial spokes */}
      {Array.from({length: 24}).map((_, i) => {
        const a = (i / 24) * Math.PI * 2;
        const x1 = 50 + Math.cos(a) * 14, y1 = 50 + Math.sin(a) * 14;
        const x2 = 50 + Math.cos(a) * 24, y2 = 50 + Math.sin(a) * 24;
        return <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="#4dd6ff" strokeOpacity={0.25 + (i%3)*0.15} strokeWidth="0.8" />;
      })}
      {/* iris fill */}
      <circle cx="50" cy="50" r="18" fill="url(#iris-grad)" stroke="#4dd6ff" strokeWidth="0.8" strokeOpacity="0.5" />
      {/* pupil */}
      <circle cx="50" cy="50" r="6" fill="#06080d" />
      <circle cx="50" cy="50" r="6" fill="none" stroke="#4dd6ff" strokeWidth="0.5" strokeOpacity="0.6" />
      {/* highlight */}
      <circle cx="46" cy="46" r="1.5" fill="#4dd6ff" opacity="0.8" />
      {/* scan arc */}
      {animated && (
        <circle className="scan" cx="50" cy="50" r="36" fill="none" stroke="#4dd6ff" strokeWidth="2"
          strokeLinecap="round" />
      )}
    </svg>
  );
};

/* ============ Splash screen ============ */
const Splash = ({ onDone }) => {
  const [step, setStep] = React.useState("Initialising sensors");
  React.useEffect(() => {
    const steps = [
      [350, "Calibrating optics"],
      [800, "Loading correlation graph"],
      [1300, "Linking 47 devices"],
      [1700, "Sync complete"],
    ];
    const timers = steps.map(([t, s]) => setTimeout(() => setStep(s), t));
    const done = setTimeout(onDone, 2300);
    return () => { timers.forEach(clearTimeout); clearTimeout(done); };
  }, []);
  return (
    <div className="splash">
      <div className="splash-inner">
        <IrisMark size={140} animated />
        <h1>N E T R A &nbsp;·&nbsp; A I</h1>
        <div className="deva">नेत्र</div>
        <div className="progress"><i /></div>
        <div className="step">{step}…</div>
      </div>
    </div>
  );
};

/* ============ Status dot ============ */
const StatusDot = ({ status }) => {
  const map = { ok: "var(--good)", live: "var(--good)", warn: "var(--warn)", crit: "var(--crit)", down: "var(--ink-3)", off: "var(--ink-3)", degr: "var(--warn)", info: "var(--info)" };
  return <span style={{ width: 8, height: 8, borderRadius: 999, display: "inline-block", background: map[status] || "var(--ink-3)", boxShadow: status === "ok" || status === "live" ? "0 0 0 4px rgba(92,224,168,0.12)" : "none" }} />;
};

/* ============ KPI card ============ */
const KPI = ({ label, value, unit, delta, deltaKind, spark, sparkColor }) => (
  <div className="kpi">
    <div className="lbl">{label}</div>
    <div className="val">{value}{unit && <small>{unit}</small>}</div>
    {delta && <div className={`delta ${deltaKind || "up"}`}>{delta}</div>}
    {spark && <div className="spark"><Sparkline data={spark} color={sparkColor || "var(--iris)"} w={90} h={28} /></div>}
  </div>
);

/* ============ Sev pill ============ */
const SevPill = ({ sev }) => {
  const map = { crit: ["crit", "Critical"], warn: ["warn", "Warning"], info: ["info", "Info"], good: ["good", "OK"] };
  const [cls, lbl] = map[sev] || ["", sev];
  return <span className={`pill ${cls}`}><span className="d" />{lbl}</span>;
};

/* ============ Avatar ============ */
const Avatar = ({ initials, size = 28, color }) => (
  <span style={{
    width: size, height: size, borderRadius: "50%",
    display: "inline-grid", placeItems: "center",
    background: color || "linear-gradient(135deg, #4dd6ff33, #f0a73a33)",
    border: "1px solid var(--line-2)",
    color: "var(--ink-0)", fontSize: size * 0.4, fontWeight: 500,
    fontFamily: "var(--font-mono)",
    flex: "none",
  }}>{initials}</span>
);

Object.assign(window, { Icon, Sparkline, IrisMark, Splash, StatusDot, KPI, SevPill, Avatar });
