/* PAAI graphics — feathered greyscale SVG visualizations */

const featherMask = {
  WebkitMaskImage:
    "radial-gradient(ellipse at center, rgba(0,0,0,1) 50%, rgba(0,0,0,0.65) 78%, rgba(0,0,0,0) 100%)",
  maskImage:
    "radial-gradient(ellipse at center, rgba(0,0,0,1) 50%, rgba(0,0,0,0.65) 78%, rgba(0,0,0,0) 100%)",
};

/* ============================================================
   Data-flow comparison — Public LLM (top, faded) vs PAAI (bottom)
   ============================================================ */
function DataFlowDiagram() {
  return (
    <svg viewBox="0 0 1200 360" width="100%" preserveAspectRatio="xMidYMid meet"
         style={{ display: "block" }}>
      <defs>
        <marker id="arrow-grey" viewBox="0 0 10 10" refX="9" refY="5"
                markerWidth="6" markerHeight="6" orient="auto-start-reverse">
          <path d="M0,0 L10,5 L0,10 z" fill="#0a0a0a" />
        </marker>
        <marker id="arrow-acc" viewBox="0 0 10 10" refX="9" refY="5"
                markerWidth="6" markerHeight="6" orient="auto-start-reverse">
          <path d="M0,0 L10,5 L0,10 z" fill="var(--acc)" />
        </marker>
        <linearGradient id="fade-h" x1="0" x2="1">
          <stop offset="0" stopColor="#ffffff" stopOpacity="0" />
          <stop offset="0.1" stopColor="#ffffff" stopOpacity="1" />
          <stop offset="0.9" stopColor="#ffffff" stopOpacity="1" />
          <stop offset="1" stopColor="#ffffff" stopOpacity="0" />
        </linearGradient>
        <mask id="fade-mask">
          <rect x="0" y="0" width="1200" height="360" fill="url(#fade-h)" />
        </mask>
      </defs>

      <g mask="url(#fade-mask)">
        {/* --- TOP LANE: Conventional / Public --- */}
        <g opacity="0.85">
          <text x="20" y="38" fontFamily="JetBrains Mono, monospace" fontSize="11"
                letterSpacing="2" fill="#666" style={{ textTransform: "uppercase" }}>
            CONVENTIONAL · DATA LEAVES THE PERIMETER
          </text>
          <line x1="20" y1="50" x2="1180" y2="50" stroke="#d6d6d6" strokeWidth="1" />

          {/* Nodes */}
          {[
            { x: 100, label: "USER" },
            { x: 320, label: "PUBLIC INTERNET", warn: true },
            { x: 560, label: "3RD-PARTY API", warn: true },
            { x: 800, label: "MODEL HOST", warn: true },
            { x: 1040, label: "LOG STORE", warn: true },
          ].map((n, i) => (
            <g key={i}>
              <rect x={n.x - 50} y={90} width={100} height={42} fill="none"
                    stroke="#0a0a0a" strokeWidth="1" strokeDasharray={n.warn ? "3 3" : "none"} />
              <text x={n.x} y={115} textAnchor="middle"
                    fontFamily="JetBrains Mono, monospace" fontSize="9"
                    letterSpacing="1.5" fill="#0a0a0a">
                {n.label}
              </text>
              {n.warn && (
                <text x={n.x + 38} y={84} textAnchor="middle"
                      fontFamily="JetBrains Mono, monospace" fontSize="11"
                      fill="var(--acc)" fontWeight="700">!</text>
              )}
            </g>
          ))}

          {/* Connecting dashed arrows */}
          {[150, 370, 610, 850].map((x, i) => (
            <line key={i} x1={x} y1={111} x2={x + 120} y2={111}
                  stroke="#0a0a0a" strokeWidth="1" strokeDasharray="4 4"
                  markerEnd="url(#arrow-grey)" opacity="0.5" />
          ))}

          {/* Status row */}
          <text x="20" y="160" fontFamily="JetBrains Mono, monospace" fontSize="9.5"
                letterSpacing="1.5" fill="#666">
            DATA EXFIL · LOG RETENTION · MODEL DRIFT · 3RD-PARTY TERMS
          </text>
        </g>

        {/* Separator */}
        <line x1="20" y1="195" x2="1180" y2="195" stroke="#0a0a0a" strokeWidth="2" />
        <text x="600" y="190" textAnchor="middle"
              fontFamily="JetBrains Mono, monospace" fontSize="9"
              letterSpacing="2" fill="#666"
              style={{ textTransform: "uppercase" }}>
          ▼ PAAI ARCHITECTURE
        </text>

        {/* --- BOTTOM LANE: PAAI --- */}
        <g transform="translate(0,30)">
          <text x="20" y="195" fontFamily="JetBrains Mono, monospace" fontSize="11"
                letterSpacing="2" fill="#0a0a0a">
            PAAI · DATA NEVER LEAVES YOUR PERIMETER
          </text>
          <line x1="20" y1="207" x2="1180" y2="207" stroke="#0a0a0a" strokeWidth="1" />

          {/* Perimeter boundary */}
          <rect x="60" y="232" width="1080" height="78" fill="none"
                stroke="#0a0a0a" strokeWidth="1.5" strokeDasharray="6 4" />
          <text x="78" y="225" fontFamily="JetBrains Mono, monospace" fontSize="9"
                letterSpacing="1.5" fill="#666">
            CLIENT PERIMETER · ENCRYPTED AT REST + IN TRANSIT
          </text>

          {/* Nodes inside perimeter */}
          {[
            { x: 160, label: "USER" },
            { x: 360, label: "mTLS GATEWAY" },
            { x: 560, label: "LOCAL LLM" },
            { x: 760, label: "AGENT HARNESS" },
            { x: 960, label: "AUDIT LOG" },
          ].map((n, i) => (
            <g key={i}>
              <rect x={n.x - 55} y={253} width={110} height={42}
                    fill="#0a0a0a" />
              <text x={n.x} y={278} textAnchor="middle"
                    fontFamily="JetBrains Mono, monospace" fontSize="9"
                    letterSpacing="1.5" fill="#fff">
                {n.label}
              </text>
            </g>
          ))}

          {/* Connecting solid arrows */}
          {[215, 415, 615, 815].map((x, i) => (
            <line key={i} x1={x} y1={274} x2={x + 90} y2={274}
                  stroke="#0a0a0a" strokeWidth="1.5"
                  markerEnd="url(#arrow-grey)" />
          ))}

          {/* Accent marker — encrypted flow indicator */}
          <g>
            <rect x="60" y="232" width="6" height="78" fill="var(--acc)" />
          </g>
        </g>
      </g>
    </svg>
  );
}

/* ============================================================
   Encryption ladder — vertical bars showing cipher strength
   ============================================================ */
function EncryptionLadder() {
  const items = [
    { label: "TLS 1.3",      spec: "TRANSPORT",      height: 30, mark: "✓" },
    { label: "AES-256-GCM",  spec: "AT-REST",        height: 55, mark: "✓" },
    { label: "GCM-SIV",      spec: "DETERMINISTIC",  height: 72, mark: "✓" },
    { label: "ChaCha20",     spec: "STREAM",         height: 78, mark: "✓" },
    { label: "ML-KEM-768",   spec: "PQ · KYBER",     height: 90, mark: "QR", accent: true },
    { label: "ML-DSA-87",    spec: "PQ · DILITHIUM", height: 98, mark: "QR", accent: true },
    { label: "ML-KEM-1024",  spec: "PQ · MAX",       height: 108, mark: "QR", accent: true },
  ];
  const N = items.length;
  const W = 760;
  const padLeft = 36;
  const padRight = 24;
  const colW = (W - padLeft - padRight) / N;
  const barW = Math.min(colW - 18, 56);
  return (
    <svg viewBox={`0 0 ${W} 200`} width="100%" preserveAspectRatio="xMidYMid meet"
         style={{ display: "block" }}>
      <line x1="0" y1="148" x2={W} y2="148" stroke="#0a0a0a" strokeWidth="1" />
      {items.map((it, i) => {
        const cx = padLeft + colW * (i + 0.5);
        const x = cx - barW / 2;
        const y = 148 - it.height;
        return (
          <g key={i}>
            <rect x={x} y={y} width={barW} height={it.height}
                  fill={it.accent ? "var(--acc)" : "#0a0a0a"} />
            <text x={cx} y={y - 6} textAnchor="middle"
                  fontFamily="JetBrains Mono, monospace" fontSize="9"
                  fontWeight="700" fill={it.accent ? "var(--acc)" : "#0a0a0a"}>
              {it.mark}
            </text>
            <text x={cx} y={166} textAnchor="middle"
                  fontFamily="Archivo, sans-serif" fontSize="11"
                  fontWeight="700" fill="#0a0a0a"
                  style={{ textTransform: "uppercase" }}>
              {it.label}
            </text>
            <text x={cx} y={180} textAnchor="middle"
                  fontFamily="JetBrains Mono, monospace" fontSize="8"
                  letterSpacing="1.5" fill="#666">
              {it.spec}
            </text>
          </g>
        );
      })}
      {/* Classical / PQ divider — between item 3 (idx) and 4 */}
      {(() => {
        const dx = padLeft + colW * 4;
        return (
          <g>
            <line x1={dx} y1="30" x2={dx} y2="148"
                  stroke="#0a0a0a" strokeWidth="0.5" strokeDasharray="3 3" />
            <text x={dx - 8} y="40" textAnchor="end"
                  fontFamily="JetBrains Mono, monospace" fontSize="9"
                  letterSpacing="1.5" fill="#666">CLASSICAL</text>
            <text x={dx + 8} y="40" textAnchor="start"
                  fontFamily="JetBrains Mono, monospace" fontSize="9"
                  letterSpacing="1.5" fill="var(--acc)">POST-QUANTUM</text>
          </g>
        );
      })()}
    </svg>
  );
}

/* ============================================================
   Quantum readiness gauge — semi-circular meter (top half)
   ============================================================ */
function QuantumGauge() {
  // Top-semicircle gauge. 0% = left (180°), 100% = right (360°/0°)
  const cx = 200, cy = 180, r = 130;
  const value = 0.88;
  // SVG angle: y is down, so 180° = left, 270° = top, 360°/0° = right
  const angleDeg = 180 + 180 * value;
  const rad = (angleDeg * Math.PI) / 180;

  const tipX = cx + Math.cos(rad) * (r - 14);
  const tipY = cy + Math.sin(rad) * (r - 14);
  const endX = cx + Math.cos(rad) * r;
  const endY = cy + Math.sin(rad) * r;

  // Ticks
  const ticks = [];
  for (let i = 0; i <= 10; i++) {
    const a = 180 + (180 * i) / 10;
    const ar = (a * Math.PI) / 180;
    const inner = r - 10;
    const outer = r;
    ticks.push({
      x1: cx + Math.cos(ar) * inner,
      y1: cy + Math.sin(ar) * inner,
      x2: cx + Math.cos(ar) * outer,
      y2: cy + Math.sin(ar) * outer,
      major: i % 2 === 0,
    });
  }

  return (
    <svg viewBox="0 0 400 230" width="100%" preserveAspectRatio="xMidYMid meet"
         style={{ display: "block" }}>
      {/* Outer arc */}
      <path d={`M ${cx - r} ${cy} A ${r} ${r} 0 0 1 ${cx + r} ${cy}`}
            fill="none" stroke="#0a0a0a" strokeWidth="2" />

      {/* Filled portion — from left edge clockwise (sweep=1, short arc) to current */}
      <path d={`M ${cx - r} ${cy} A ${r} ${r} 0 0 1 ${endX} ${endY}`}
            fill="none" stroke="#0a0a0a" strokeWidth="8" />

      {/* Ticks */}
      {ticks.map((t, i) => (
        <line key={i} x1={t.x1} y1={t.y1} x2={t.x2} y2={t.y2}
              stroke="#0a0a0a" strokeWidth={t.major ? 1.5 : 0.7} />
      ))}

      {/* Endpoint labels — placed below the arc terminals so they stay inside the viewBox */}
      <text x={cx - r} y={cy + 18} textAnchor="middle"
            fontFamily="JetBrains Mono, monospace" fontSize="9.5"
            letterSpacing="1.5" fill="#666">CLASSICAL</text>
      <text x={cx + r} y={cy + 18} textAnchor="middle"
            fontFamily="JetBrains Mono, monospace" fontSize="9.5"
            letterSpacing="1.5" fill="var(--acc)">QR</text>

      {/* Needle */}
      <line x1={cx} y1={cy} x2={tipX} y2={tipY}
            stroke="var(--acc)" strokeWidth="3" />
      <circle cx={cx} cy={cy} r="6" fill="#0a0a0a" />
      <circle cx={cx} cy={cy} r="2.5" fill="var(--acc)" />

      {/* Value — placed above the pivot, inside the arc */}
      <text x={cx} y={cy - 50} textAnchor="middle"
            fontFamily="Archivo, sans-serif" fontSize="38" fontWeight="900"
            fill="#0a0a0a" letterSpacing="-0.02em">
        88%
      </text>
      <text x={cx} y={cy - 32} textAnchor="middle"
            fontFamily="JetBrains Mono, monospace" fontSize="9"
            letterSpacing="2" fill="#666">
        DEPLOYED CIPHERS · PQ-READY
      </text>
    </svg>
  );
}

/* ============================================================
   Stack diagram — vertical layered stack (App → Hardware)
   ============================================================ */
function StackDiagram() {
  const layers = [
    { label: "APPLICATION",       meta: "Your apps · APIs · SDKs",        h: 40 },
    { label: "AGENT HARNESS",     meta: "Tools · memory · audit",         h: 40, acc: 1 },
    { label: "INFERENCE",         meta: "Local LLM · vLLM · TGI",         h: 40 },
    { label: "TRANSPORT",         meta: "mTLS 1.3 · ZTNA · WireGuard",    h: 40, acc: 0.5 },
    { label: "ENCRYPTION MODULE", meta: "FIPS 140-3 · HSM · KMS",         h: 40 },
    { label: "HARDWARE",          meta: "On-prem GPU · TPM · Air-gap",    h: 40 },
  ];
  let y = 30;
  return (
    <svg viewBox="0 0 420 320" width="100%" preserveAspectRatio="xMidYMid meet"
         style={{ display: "block" }}>
      <text x="0" y="18" fontFamily="JetBrains Mono, monospace" fontSize="11"
            letterSpacing="2" fill="#666">
        FIG. 03 · PAAI STACK · TOP-DOWN
      </text>
      {layers.map((l, i) => {
        // acc: 0/undefined → plain white; 0.5 → diffuse accent; 1 → solid accent
        const isSolid = l.acc === 1;
        const isDiffuse = l.acc === 0.5;
        const isAcc = isSolid || isDiffuse;
        const textColor = isSolid ? "#fff" : "#0a0a0a";
        const metaColor = isSolid ? "#fff" : isDiffuse ? "#0a0a0a" : "#666";
        const row = (
          <g key={i}>
            <rect
              x="0" y={y} width="420" height={l.h}
              fill={isAcc ? "var(--acc)" : "#fff"}
              fillOpacity={isDiffuse ? 0.5 : 1}
              stroke="#0a0a0a" strokeWidth="1"
            />
            <text x="14" y={y + 18}
                  fontFamily="Archivo, sans-serif" fontSize="13"
                  fontWeight="900" fill={textColor}
                  style={{ textTransform: "uppercase" }}>
              {l.label}
            </text>
            <text x="14" y={y + 33}
                  fontFamily="JetBrains Mono, monospace" fontSize="9.5"
                  letterSpacing="1.2" fill={metaColor}>
              {l.meta}
            </text>
            <text x="406" y={y + 25} textAnchor="end"
                  fontFamily="JetBrains Mono, monospace" fontSize="9"
                  letterSpacing="1.2" fill={metaColor}>
              L{layers.length - i}
            </text>
          </g>
        );
        y += l.h + 2;
        return row;
      })}
    </svg>
  );
}

/* ============================================================
   Threat surface radar — comparing public vs PAAI
   ============================================================ */
function ThreatRadar() {
  const axes = [
    "DATA EXFIL", "MODEL POISON", "PROMPT LEAK",
    "LOG RETENTION", "VENDOR LOCK", "REGULATORY", "QUANTUM"
  ];
  // public values higher (more risk), PAAI values lower
  const publicV = [0.85, 0.6, 0.78, 0.9, 0.7, 0.82, 0.95];
  const paaiV   = [0.15, 0.22, 0.18, 0.1, 0.2, 0.25, 0.18];
  const cx = 180, cy = 180, r = 120;
  const N = axes.length;
  const angle = (i) => -Math.PI / 2 + (i / N) * Math.PI * 2;
  const point = (i, v) => [cx + Math.cos(angle(i)) * r * v, cy + Math.sin(angle(i)) * r * v];

  const polyStr = (vs) => vs.map((v, i) => point(i, v).join(",")).join(" ");

  return (
    <svg viewBox="0 0 360 360" width="100%" preserveAspectRatio="xMidYMid meet"
         style={{ display: "block" }}>
      <text x="0" y="18" fontFamily="JetBrains Mono, monospace" fontSize="11"
            letterSpacing="2" fill="#666">
        FIG. 06 · THREAT SURFACE · PUBLIC vs PAAI
      </text>
      {/* Concentric rings */}
      {[0.25, 0.5, 0.75, 1].map((k, i) => (
        <circle key={i} cx={cx} cy={cy} r={r * k}
                fill="none" stroke="#d6d6d6" strokeWidth="0.7" />
      ))}
      {/* Axes */}
      {axes.map((a, i) => {
        const [x, y] = point(i, 1);
        return (
          <g key={i}>
            <line x1={cx} y1={cy} x2={x} y2={y} stroke="#d6d6d6" strokeWidth="0.7" />
          </g>
        );
      })}
      {/* Public polygon */}
      <polygon points={polyStr(publicV)}
               fill="#0a0a0a" fillOpacity="0.1"
               stroke="#0a0a0a" strokeWidth="1" strokeDasharray="4 3" />
      {/* PAAI polygon */}
      <polygon points={polyStr(paaiV)}
               fill="var(--acc)" fillOpacity="0.2"
               stroke="var(--acc)" strokeWidth="1.5" />
      {/* Labels */}
      {axes.map((a, i) => {
        const [x, y] = point(i, 1.18);
        return (
          <text key={i} x={x} y={y} textAnchor="middle" dominantBaseline="middle"
                fontFamily="JetBrains Mono, monospace" fontSize="8.5"
                letterSpacing="1.4" fill="#0a0a0a">
            {a}
          </text>
        );
      })}
      {/* Legend */}
      <g transform="translate(0,330)">
        <line x1="0" y1="0" x2="14" y2="0" stroke="#0a0a0a"
              strokeWidth="1" strokeDasharray="4 3" />
        <text x="20" y="3" fontFamily="JetBrains Mono, monospace" fontSize="9"
              letterSpacing="1.2" fill="#666">PUBLIC</text>
        <line x1="100" y1="0" x2="114" y2="0" stroke="var(--acc)" strokeWidth="1.5" />
        <text x="120" y="3" fontFamily="JetBrains Mono, monospace" fontSize="9"
              letterSpacing="1.2" fill="var(--acc)">PAAI</text>
      </g>
    </svg>
  );
}

Object.assign(window, {
  DataFlowDiagram,
  EncryptionLadder,
  QuantumGauge,
  StackDiagram,
  ThreatRadar,
});
