/* cctv.ae — high-quality graphics & simulation concepts.
   Premium icon chips, a radar analytics HUD, a live camera-network graph,
   and four designed industry illustrations (replacing the photo tiles). */
(function () {
  const CY = "var(--af-light-blue)";

  /* Premium gradient icon chip (replaces flat icon tiles everywhere). */
  function IconChip({ name, size = 26, chip = 56, onDark = false, stroke = 1.6 }) {
    const Icon = window.CCTVIcon;
    return (
      <span style={{ position: "relative", display: "grid", placeItems: "center", width: chip, height: chip, borderRadius: "30%", flexShrink: 0,
        background: onDark ? "linear-gradient(150deg, rgba(0,174,239,0.22), rgba(0,91,170,0.10))" : "linear-gradient(150deg, rgba(0,174,239,0.16), rgba(0,91,170,0.07))",
        boxShadow: onDark ? "inset 0 0 0 1px rgba(0,174,239,0.35)" : "inset 0 0 0 1px rgba(0,91,170,0.16)" }}>
        <span aria-hidden style={{ position: "absolute", top: 6, left: 6, width: 10, height: 10, borderTop: `2px solid ${CY}`, borderLeft: `2px solid ${CY}`, opacity: 0.8 }} />
        <span aria-hidden style={{ position: "absolute", bottom: 6, right: 6, width: 10, height: 10, borderBottom: `2px solid ${CY}`, borderRight: `2px solid ${CY}`, opacity: 0.8 }} />
        <Icon name={name} size={size} color={onDark ? "#fff" : "var(--af-dark-blue)"} stroke={stroke} />
      </span>
    );
  }

  /* Hero — radar analytics HUD (replaces the horizontal scan-line concept). */
  function RadarHUD() {
    const HudChip = window.CCTVHudChip;
    const DetBox = window.CCTVDetBox;
    const blips = [[68, 30], [40, 60], [78, 64], [55, 42]];
    return (
      <div aria-hidden className="ai-layer" style={{ position: "absolute", inset: 0, overflow: "hidden", pointerEvents: "none", zIndex: 2 }}>
        <div style={{ position: "absolute", top: "12%", right: "3.5%", display: "flex", flexDirection: "column", gap: 8, alignItems: "flex-end" }}>
          <HudChip>AI Vision · Live</HudChip>
          <HudChip style={{ textTransform: "none" }}>14 feeds · 6 alerts</HudChip>
        </div>
        {/* radar */}
        <div style={{ position: "absolute", right: "7%", top: "32%", width: "min(20rem,34vw)", aspectRatio: "1", borderRadius: "50%" }}>
          <svg viewBox="0 0 100 100" width="100%" height="100%" style={{ position: "absolute", inset: 0 }}>
            {[46, 33, 20, 8].map((r, i) => <circle key={i} cx="50" cy="50" r={r} fill="none" stroke="rgba(0,174,239,0.28)" strokeWidth="0.5" />)}
            <line x1="4" y1="50" x2="96" y2="50" stroke="rgba(0,174,239,0.22)" strokeWidth="0.5" />
            <line x1="50" y1="4" x2="50" y2="96" stroke="rgba(0,174,239,0.22)" strokeWidth="0.5" />
            {blips.map(([x, y], i) => <circle key={i} cx={x} cy={y} r="1.7" fill={CY} className="ai-blink" style={{ animationDelay: (i * 0.4) + "s" }} />)}
          </svg>
          <span className="radar-sweep" style={{ position: "absolute", inset: 0, borderRadius: "50%", background: "conic-gradient(from 0deg, rgba(0,174,239,0) 0deg, rgba(0,174,239,0) 295deg, rgba(0,174,239,0.45) 358deg, rgba(0,174,239,0) 360deg)" }} />
        </div>
        <DetBox left="49%" top="22%" width="13%" height="28%" label="PERSON" conf="0.98" />
        <DetBox left="60%" top="58%" width="15%" height="16%" label="PLATE · D 12345" conf="0.95" accent="#7ee0ff" labelSide="bottom" />
      </div>
    );
  }

  /* Threats — live camera-network topology simulation. */
  function CameraNetwork({ items }) {
    // node positions (%), some flagged by severity index into items
    const nodes = [
      { x: 12, y: 30 }, { x: 26, y: 62 }, { x: 30, y: 18 }, { x: 46, y: 44 },
      { x: 44, y: 80 }, { x: 62, y: 24 }, { x: 66, y: 60 }, { x: 80, y: 40 },
      { x: 84, y: 74 }, { x: 90, y: 18 },
    ];
    const edges = [[0, 3], [2, 3], [1, 3], [3, 5], [3, 6], [4, 6], [5, 7], [6, 7], [7, 9], [7, 8], [6, 4], [5, 9]];
    const flagged = { 5: "Critical", 6: "High", 8: "Critical", 2: "Medium", 1: "High" };
    const col = (s) => (s === "Critical" ? "#ff6168" : s === "High" ? "#ffb020" : CY);
    return (
      <svg viewBox="0 0 100 64" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" style={{ display: "block" }} aria-hidden>
        <defs>
          <radialGradient id="netGlow" cx="50%" cy="40%" r="70%">
            <stop offset="0%" stopColor="rgba(0,174,239,0.16)" /><stop offset="100%" stopColor="rgba(0,174,239,0)" />
          </radialGradient>
        </defs>
        <rect x="0" y="0" width="100" height="64" fill="url(#netGlow)" />
        {edges.map(([a, b], i) => <line key={i} x1={nodes[a].x} y1={nodes[a].y} x2={nodes[b].x} y2={nodes[b].y} stroke="rgba(120,180,230,0.28)" strokeWidth="0.4" />)}
        {nodes.map((n, i) => {
          const sev = flagged[i];
          const c = sev ? col(sev) : "rgba(150,200,235,0.7)";
          return (
            <g key={i}>
              {sev && <circle cx={n.x} cy={n.y} r="3.4" fill="none" stroke={c} strokeWidth="0.5" className="net-pulse" style={{ animationDelay: (i * 0.5) + "s", transformOrigin: `${n.x}px ${n.y}px` }} />}
              <circle cx={n.x} cy={n.y} r="1.7" fill={c} />
              <rect x={n.x - 1.5} y={n.y - 1.4} width="3" height="2.4" rx="0.5" fill="#041a3a" stroke={c} strokeWidth="0.35" />
              <circle cx={n.x + 0.5} cy={n.y - 0.2} r="0.6" fill={c} />
            </g>
          );
        })}
      </svg>
    );
  }

  /* ── Industry illustrations (designed, brand duotone) ───── */
  const Frame = ({ id, children, accent = CY }) => (
    <svg viewBox="0 0 160 100" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{ display: "block" }} aria-hidden>
      <defs>
        <linearGradient id={id + "bg"} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#0a2c57" /><stop offset="100%" stopColor="#04132c" />
        </linearGradient>
        <linearGradient id={id + "cy"} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#7ee0ff" /><stop offset="100%" stopColor="#00aeef" />
        </linearGradient>
      </defs>
      <rect width="160" height="100" fill={`url(#${id}bg)`} />
      <g opacity="0.5" stroke="rgba(0,174,239,0.18)" strokeWidth="0.5">
        {[20, 40, 60, 80].map((y) => <line key={y} x1="0" y1={y} x2="160" y2={y} />)}
        {[40, 80, 120].map((x) => <line key={x} x1={x} y1="0" x2={x} y2="100" />)}
      </g>
      {children}
      {/* FR detection accent */}
      <g stroke={accent} strokeWidth="1.2" fill="none">
        <path d="M120 18 h-8 v8 M120 18 v8" /><path d="M120 18 h8 v8" transform="translate(0,0)" />
      </g>
    </svg>
  );

  const Schools = () => (
    <Frame id="sch">
      <rect x="20" y="60" width="120" height="6" rx="2" fill="url(#schcy)" opacity="0.85" />
      <rect x="30" y="30" width="42" height="26" rx="2" fill="#0e3a6e" stroke="#7ee0ff" strokeWidth="1" />
      <line x1="34" y1="38" x2="62" y2="38" stroke="#7ee0ff" strokeWidth="1.4" /><line x1="34" y1="44" x2="56" y2="44" stroke="#bfe9ff" strokeWidth="1" opacity="0.7" />
      <g fill="#9fd8f5"><circle cx="92" cy="44" r="6" /><rect x="86" y="52" width="12" height="12" rx="3" /></g>
      <g fill="#cdeeff"><circle cx="110" cy="46" r="5" /><rect x="105" y="53" width="10" height="11" rx="3" /></g>
      <circle cx="92" cy="44" r="9" fill="none" stroke="#00aeef" strokeWidth="1" />
    </Frame>
  );
  const Visitor = () => (
    <Frame id="vis">
      <rect x="22" y="58" width="116" height="20" rx="3" fill="#0e3a6e" />
      <rect x="22" y="58" width="116" height="5" fill="url(#viscy)" />
      <rect x="30" y="30" width="26" height="20" rx="2" fill="#0e3a6e" stroke="#7ee0ff" strokeWidth="1" />
      <circle cx="43" cy="38" r="3.4" fill="#7ee0ff" /><rect x="37" y="42" width="12" height="6" rx="2" fill="#7ee0ff" opacity="0.8" />
      <g fill="#9fd8f5"><circle cx="96" cy="36" r="6.5" /><rect x="89" y="44" width="14" height="14" rx="3" /></g>
      <rect x="112" y="40" width="14" height="20" rx="2" fill="#0e3a6e" stroke="#00aeef" strokeWidth="1" /><circle cx="119" cy="46" r="2.4" fill="#00aeef" />
      <circle cx="96" cy="36" r="9.5" fill="none" stroke="#00aeef" strokeWidth="1" />
    </Frame>
  );
  const Retail = () => (
    <Frame id="ret">
      <rect x="20" y="62" width="120" height="6" rx="2" fill="url(#retcy)" opacity="0.85" />
      <g fill="#0e3a6e" stroke="#7ee0ff" strokeWidth="1"><rect x="28" y="26" width="20" height="34" rx="2" /><rect x="52" y="26" width="20" height="34" rx="2" /></g>
      {[30, 38, 46].map((y) => <line key={y} x1="30" y1={y} x2="46" y2={y} stroke="#7ee0ff" strokeWidth="1" opacity="0.6" />)}
      {[30, 38, 46].map((y) => <line key={"b" + y} x1="54" y1={y} x2="70" y2={y} stroke="#7ee0ff" strokeWidth="1" opacity="0.6" />)}
      <path d="M92 34 h6 l4 18 h18 l3 -12 h-22" fill="none" stroke="#9fd8f5" strokeWidth="1.6" /><circle cx="104" cy="58" r="2.6" fill="#9fd8f5" /><circle cx="118" cy="58" r="2.6" fill="#9fd8f5" />
      <g fill="#cdeeff"><circle cx="126" cy="40" r="5" /><rect x="121" y="47" width="10" height="11" rx="3" /></g>
      <circle cx="126" cy="40" r="8.5" fill="none" stroke="#00aeef" strokeWidth="1" />
    </Frame>
  );
  const Facilities = () => (
    <Frame id="fac">
      <rect x="20" y="64" width="120" height="6" rx="2" fill="url(#faccy)" opacity="0.85" />
      <rect x="30" y="24" width="44" height="40" fill="#0e3a6e" stroke="#7ee0ff" strokeWidth="1" />
      {[30, 42, 54].map((y) => [38, 50, 62].map((x) => <rect key={x + "-" + y} x={x} y={y} width="6" height="6" fill="#7ee0ff" opacity="0.55" />))}
      <rect x="86" y="40" width="26" height="24" fill="#0e3a6e" stroke="#7ee0ff" strokeWidth="1" />
      <path d="M124 30 l12 4 v9 c0 8 -6 13 -12 16 c-6 -3 -12 -8 -12 -16 v-9 z" fill="#0e3a6e" stroke="#00aeef" strokeWidth="1.2" />
      <path d="M119 44 l4 4 7 -8" fill="none" stroke="#7ee0ff" strokeWidth="1.6" strokeLinecap="round" />
    </Frame>
  );

  Object.assign(window, {
    CCTVIconChip: IconChip,
    CCTVRadarHUD: RadarHUD,
    CCTVCameraNetwork: CameraNetwork,
    CCTVIllos: { schools: Schools, visitor: Visitor, retail: Retail, facilities: Facilities },
    CCTVAIControlRoom: AIControlRoom,
  });

  /* ── AI control-room illustration (designed, brand duotone) ─ */
  function AIControlRoom() {
    return (
      <svg viewBox="0 0 640 300" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{ display: "block" }} role="img" aria-label="AI-assisted CCTV control room">
        <defs>
          <linearGradient id="acrbg" x1="0" y1="0" x2="0" y2="1"><stop offset="0%" stopColor="#0a2c57" /><stop offset="100%" stopColor="#03112a" /></linearGradient>
          <linearGradient id="acrscreen" x1="0" y1="0" x2="1" y2="1"><stop offset="0%" stopColor="#0e3a6e" /><stop offset="100%" stopColor="#072144" /></linearGradient>
          <radialGradient id="acrglow" cx="50%" cy="20%" r="80%"><stop offset="0%" stopColor="rgba(0,174,239,0.30)" /><stop offset="100%" stopColor="rgba(0,174,239,0)" /></radialGradient>
          <linearGradient id="acrcy" x1="0" y1="0" x2="1" y2="0"><stop offset="0%" stopColor="#7ee0ff" /><stop offset="100%" stopColor="#00aeef" /></linearGradient>
        </defs>
        <rect width="640" height="300" fill="url(#acrbg)" />
        <rect width="640" height="300" fill="url(#acrglow)" />
        {/* video wall */}
        <g>
          {[[40, 26], [232, 26], [424, 26]].map(([x, y], i) => (
            <g key={i}>
              <rect x={x} y={y} width="176" height="104" rx="6" fill="url(#acrscreen)" stroke="rgba(0,174,239,0.45)" strokeWidth="1.5" />
              <rect x={x + 10} y={y + 12} width="80" height="6" rx="3" fill="rgba(126,224,255,0.5)" />
              {/* face/box detections per screen */}
              {i === 0 && <g><rect x={x + 30} y={y + 34} width="34" height="44" rx="2" fill="none" stroke="#7ee0ff" strokeWidth="2" /><circle cx={x + 47} cy={y + 50} r="8" fill="rgba(126,224,255,0.35)" /><rect x={x + 100} y={y + 60} width="48" height="26" rx="2" fill="none" stroke="#ffb020" strokeWidth="2" /></g>}
              {i === 1 && <g><circle cx={x + 88} cy={y + 56} r="22" fill="none" stroke="#7ee0ff" strokeWidth="1.5" /><circle cx={x + 88} cy={y + 56} r="13" fill="none" stroke="#7ee0ff" strokeWidth="1" /><line x1={x + 60} y1={y + 56} x2={x + 116} y2={y + 56} stroke="rgba(126,224,255,0.6)" strokeWidth="1" /><line x1={x + 88} y1={y + 30} x2={x + 88} y2={y + 84} stroke="rgba(126,224,255,0.6)" strokeWidth="1" /></g>}
              {i === 2 && <g><rect x={x + 26} y={y + 70} width="60" height="20" rx="3" fill="none" stroke="#7ee0ff" strokeWidth="2" /><text x={x + 56} y={y + 85} fontFamily="ui-monospace, monospace" fontSize="12" fontWeight="700" fill="#7ee0ff" textAnchor="middle">D 12345</text><path d={`M${x + 110} ${y + 84} q14 -30 40 -8`} fill="none" stroke="#ffb020" strokeWidth="2" /></g>}
            </g>
          ))}
        </g>
        {/* central AI core + neural links to screens */}
        <g>
          <line x1="128" y1="130" x2="320" y2="190" stroke="rgba(0,174,239,0.4)" strokeWidth="1" /><line x1="320" y1="130" x2="320" y2="190" stroke="rgba(0,174,239,0.4)" strokeWidth="1" /><line x1="512" y1="130" x2="320" y2="190" stroke="rgba(0,174,239,0.4)" strokeWidth="1" />
          <circle cx="320" cy="196" r="26" fill="#04122a" stroke="url(#acrcy)" strokeWidth="2" />
          <circle cx="320" cy="196" r="26" fill="none" stroke="#00aeef" strokeWidth="1" className="net-pulse" style={{ transformOrigin: "320px 196px" }} />
          {[0, 60, 120, 180, 240, 300].map((a) => { const r = a * Math.PI / 180; return <circle key={a} cx={320 + 18 * Math.cos(r)} cy={196 + 18 * Math.sin(r)} r="2.2" fill="#7ee0ff" />; })}
          <text x="320" y="200" fontFamily="ui-monospace, monospace" fontSize="11" fontWeight="700" fill="#fff" textAnchor="middle">AI</text>
        </g>
        {/* operator desk silhouettes */}
        <g fill="#061a38" stroke="rgba(0,174,239,0.35)" strokeWidth="1.2">
          <rect x="120" y="250" width="400" height="50" rx="8" />
        </g>
        <g fill="#0a2a52">
          <circle cx="232" cy="246" r="16" /><rect x="214" y="258" width="36" height="42" rx="10" />
          <circle cx="408" cy="246" r="16" /><rect x="390" y="258" width="36" height="42" rx="10" />
        </g>
        {/* status ticker */}
        <g>
          <rect x="40" y="150" width="120" height="16" rx="8" fill="rgba(0,174,239,0.12)" stroke="rgba(0,174,239,0.4)" strokeWidth="1" />
          <circle cx="52" cy="158" r="3" fill="#37d39b" className="ai-blink" />
          <text x="62" y="162" fontFamily="ui-monospace, monospace" fontSize="9" fill="#7ee0ff">AI ANALYSING · LIVE</text>
        </g>
      </svg>
    );
  }

})();
