// Original SVG diagrams for Vraify. All hand-coded geometric primitives.

// HERO: Animated data-flow loop. Expert → Annotation → Model → Eval, with verification feedback.
function HeroFlow() {
  const [tick, setTick] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setTick(t => t + 1), 60);
    return () => clearInterval(id);
  }, []);

  // Layout: 4 nodes around a verification core
  const nodes = [
    { id: "expert",  x: 90,  y: 220, label: "EXPERT",     sub: "PhD · MD · JD" },
    { id: "annot",   x: 360, y: 90,  label: "ANNOTATION", sub: "spec → label" },
    { id: "model",   x: 630, y: 220, label: "MODEL",      sub: "SFT · RLHF · DPO" },
    { id: "eval",    x: 360, y: 380, label: "EVALUATION", sub: "verifier · judge" },
  ];
  const core = { x: 360, y: 235 };

  // packets traveling along the perimeter loop
  const perim = [
    { from: nodes[0], to: nodes[1] },
    { from: nodes[1], to: nodes[2] },
    { from: nodes[2], to: nodes[3] },
    { from: nodes[3], to: nodes[0] },
  ];

  function pointOn(seg, t) {
    return { x: seg.from.x + (seg.to.x - seg.from.x) * t, y: seg.from.y + (seg.to.y - seg.from.y) * t };
  }

  const packets = perim.flatMap((seg, i) => {
    return [0, 0.33, 0.66].map((off, j) => {
      const t = ((tick / 80) + off + i * 0.07) % 1;
      const p = pointOn(seg, t);
      return { ...p, key: `${i}-${j}`, t };
    });
  });

  // pulsing verification "spokes" from each node to core
  const spokeOpacity = (i) => 0.18 + 0.18 * Math.sin((tick / 16) + i);

  return (
    <svg viewBox="0 0 720 470" style={{ width: "100%", height: "auto", display: "block" }} aria-hidden="true">
      <defs>
        <pattern id="hero-grid" width="40" height="40" patternUnits="userSpaceOnUse">
          <path d="M 40 0 L 0 0 0 40" fill="none" stroke="var(--line)" strokeWidth="0.5" />
        </pattern>
        <radialGradient id="core-gradient">
          <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.25" />
          <stop offset="100%" stopColor="var(--accent)" stopOpacity="0" />
        </radialGradient>
      </defs>

      <rect width="720" height="470" fill="url(#hero-grid)" />

      {/* perimeter path */}
      <g>
        {perim.map((seg, i) => (
          <line key={i} x1={seg.from.x} y1={seg.from.y} x2={seg.to.x} y2={seg.to.y}
            stroke="var(--line-strong)" strokeOpacity="0.22" strokeWidth="1" strokeDasharray="2 4" />
        ))}
      </g>

      {/* spokes to core */}
      {nodes.map((n, i) => (
        <line key={n.id} x1={n.x} y1={n.y} x2={core.x} y2={core.y}
          stroke="var(--accent)" strokeOpacity={spokeOpacity(i)} strokeWidth="0.8" />
      ))}

      {/* core verification ring */}
      <circle cx={core.x} cy={core.y} r="80" fill="url(#core-gradient)" />
      <circle cx={core.x} cy={core.y} r="46" fill="none" stroke="var(--accent)" strokeOpacity="0.4" strokeWidth="1" />
      <circle cx={core.x} cy={core.y} r="62" fill="none" stroke="var(--accent)" strokeOpacity="0.18" strokeWidth="1" strokeDasharray="3 5"
        style={{ transformOrigin: `${core.x}px ${core.y}px`, transform: `rotate(${tick * 1.4}deg)` }} />

      {/* core label */}
      <g textAnchor="middle" fontFamily="var(--font-mono)" fill="var(--accent-deep)">
        <text x={core.x} y={core.y - 4} fontSize="9" letterSpacing="0.12em">VERIFICATION</text>
        <text x={core.x} y={core.y + 10} fontSize="9" letterSpacing="0.12em">CORE</text>
      </g>
      <circle cx={core.x} cy={core.y} r="3" fill="var(--accent)" />

      {/* perimeter packets */}
      {packets.map(p => (
        <circle key={p.key} cx={p.x} cy={p.y} r="2.5" fill="var(--accent)" opacity="0.9" />
      ))}

      {/* nodes */}
      {nodes.map(n => (
        <g key={n.id}>
          <rect x={n.x - 56} y={n.y - 22} width="112" height="44" rx="6"
            fill="var(--bg-elev)" stroke="var(--line-strong)" strokeOpacity="0.3" />
          <text x={n.x} y={n.y - 4} textAnchor="middle" fontFamily="var(--font-mono)" fontSize="10"
            letterSpacing="0.1em" fill="var(--ink)">{n.label}</text>
          <text x={n.x} y={n.y + 11} textAnchor="middle" fontFamily="var(--font-mono)" fontSize="9"
            letterSpacing="0.04em" fill="var(--ink-3)">{n.sub}</text>
          <circle cx={n.x - 56} cy={n.y} r="2" fill="var(--accent)" />
        </g>
      ))}

      {/* coordinates */}
      <g fontFamily="var(--font-mono)" fontSize="9" fill="var(--ink-4)" letterSpacing="0.05em">
        <text x="12" y="20">[01]</text>
        <text x="690" y="20" textAnchor="end">VRAIFY/PIPELINE.v3</text>
        <text x="12" y="460">expert→annotation→model→eval ↻</text>
        <text x="690" y="460" textAnchor="end">{`t=${(tick / 60).toFixed(1)}s`}</text>
      </g>
    </svg>
  );
}

// VETTING FUNNEL: progressive narrowing
function VettingFunnel() {
  const stages = [
    { label: "Applicants", w: 100 },
    { label: "Profile screen", w: 78 },
    { label: "AI interview", w: 56 },
    { label: "Calibration task", w: 38 },
    { label: "Active network", w: 24 },
  ];
  return (
    <svg viewBox="0 0 360 320" style={{ width: "100%", height: "auto" }} aria-hidden="true">
      {stages.map((s, i) => {
        const y = 30 + i * 54;
        const w = (s.w / 100) * 280;
        const x = 180 - w / 2;
        return (
          <g key={i}>
            <rect x={x} y={y} width={w} height="34" rx="3"
              fill={i === stages.length - 1 ? "var(--accent)" : "var(--bg-soft)"}
              stroke="var(--line-strong)" strokeOpacity={i === stages.length - 1 ? 0 : 0.25} />
            <text x="20" y={y + 21} fontFamily="var(--font-mono)" fontSize="10"
              fill="var(--ink-3)" letterSpacing="0.05em">{s.label.toUpperCase()}</text>
          </g>
        );
      })}
    </svg>
  );
}

// QA pipeline diagram
function QAPipeline() {
  const stages = ["INGEST", "TASK", "EXPERT", "REVIEW", "JUDGE", "GOLD", "DELIVER"];
  return (
    <svg viewBox="0 0 700 200" style={{ width: "100%", height: "auto" }} aria-hidden="true">
      <defs>
        <pattern id="qa-grid" width="20" height="20" patternUnits="userSpaceOnUse">
          <path d="M 20 0 L 0 0 0 20" fill="none" stroke="var(--line)" strokeWidth="0.5" />
        </pattern>
      </defs>
      <rect width="700" height="200" fill="url(#qa-grid)" />
      {stages.map((s, i) => {
        const x = 30 + i * 95;
        const isVerifier = i === 4 || i === 5;
        return (
          <g key={i}>
            <rect x={x} y="78" width="78" height="44" rx="4"
              fill={isVerifier ? "var(--accent-soft)" : "var(--bg-elev)"}
              stroke={isVerifier ? "var(--accent)" : "var(--line-strong)"}
              strokeOpacity={isVerifier ? 0.5 : 0.25} />
            <text x={x + 39} y={104} textAnchor="middle"
              fontFamily="var(--font-mono)" fontSize="10" letterSpacing="0.08em"
              fill={isVerifier ? "var(--accent-deep)" : "var(--ink)"}>{s}</text>
            {i < stages.length - 1 && (
              <g>
                <line x1={x + 78} y1="100" x2={x + 95} y2="100"
                  stroke="var(--ink-4)" strokeWidth="1" />
                <path d={`M ${x + 92} 97 L ${x + 95} 100 L ${x + 92} 103`}
                  fill="none" stroke="var(--ink-4)" strokeWidth="1" />
              </g>
            )}
            <text x={x + 39} y={68} textAnchor="middle" fontFamily="var(--font-mono)"
              fontSize="9" fill="var(--ink-4)">[0{i+1}]</text>
          </g>
        );
      })}
      {/* feedback arrow */}
      <path d="M 550 122 Q 550 170 220 170 Q 70 170 70 122"
        fill="none" stroke="var(--accent)" strokeWidth="1" strokeDasharray="3 3" opacity="0.6" />
      <text x="360" y="186" textAnchor="middle" fontFamily="var(--font-mono)"
        fontSize="9" fill="var(--accent-deep)" letterSpacing="0.08em">PROVENANCE FEEDBACK LOOP</text>
    </svg>
  );
}

// RL environment diagram: a Docker-wrapped app with reward ports
function RLEnvDiagram() {
  return (
    <svg viewBox="0 0 360 280" style={{ width: "100%", height: "auto" }} aria-hidden="true">
      <defs>
        <pattern id="rl-grid" width="12" height="12" patternUnits="userSpaceOnUse">
          <path d="M 12 0 L 0 0 0 12" fill="none" stroke="var(--line)" strokeWidth="0.4" />
        </pattern>
      </defs>
      <rect width="360" height="280" fill="url(#rl-grid)" />
      {/* outer container = sandbox */}
      <rect x="40" y="40" width="280" height="200" rx="4" fill="none"
        stroke="var(--ink)" strokeOpacity="0.6" strokeDasharray="4 4" />
      <text x="50" y="32" fontFamily="var(--font-mono)" fontSize="9"
        letterSpacing="0.1em" fill="var(--ink-3)">SANDBOX · DOCKER</text>

      {/* inner app */}
      <rect x="80" y="80" width="200" height="80" rx="3"
        fill="var(--bg-elev)" stroke="var(--line-strong)" strokeOpacity="0.3" />
      <text x="180" y="115" textAnchor="middle" fontFamily="var(--font-mono)"
        fontSize="11" letterSpacing="0.08em" fill="var(--ink)">REAL APP</text>
      <text x="180" y="132" textAnchor="middle" fontFamily="var(--font-mono)"
        fontSize="9" fill="var(--ink-3)">browser · code · tools</text>

      {/* agent */}
      <circle cx="180" cy="200" r="14" fill="var(--accent)" />
      <text x="180" y="204" textAnchor="middle" fontFamily="var(--font-mono)"
        fontSize="9" fill="var(--accent-ink)" letterSpacing="0.05em">AGT</text>
      <line x1="180" y1="186" x2="180" y2="160" stroke="var(--accent)" strokeWidth="1.2" />
      <line x1="180" y1="160" x2="120" y2="160" stroke="var(--accent)" strokeWidth="1.2" strokeOpacity="0.4" />
      <line x1="180" y1="160" x2="240" y2="160" stroke="var(--accent)" strokeWidth="1.2" strokeOpacity="0.4" />

      {/* reward ports */}
      <g fontFamily="var(--font-mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.05em">
        <circle cx="40" cy="80" r="3" fill="var(--accent)" /><text x="20" y="84" textAnchor="end">obs</text>
        <circle cx="40" cy="200" r="3" fill="var(--accent)" /><text x="20" y="204" textAnchor="end">act</text>
        <circle cx="320" cy="80" r="3" fill="var(--accent)" /><text x="340" y="84">trace</text>
        <circle cx="320" cy="200" r="3" fill="var(--green)" /><text x="340" y="204">reward</text>
      </g>
    </svg>
  );
}

Object.assign(window, { HeroFlow, VettingFunnel, QAPipeline, RLEnvDiagram });
