// AI Labs hero + spirograph SVG (the RAOR-loop concept art).
// Spirograph = hypotrochoid: a small inner gear rolls inside a large outer ring,
// tracing the curve. We map the 4 RAOR phases to the 4 cardinal points of the ring.

const Spirograph = ({ size = 320, animate = true, blend = 'multiply' }) => {
  // Hypotrochoid params — tuned to look like the reference image
  const R = 100;   // outer ring radius
  const r = 38;    // inner gear radius
  const d = 56;    // pen offset from center of inner gear
  const turns = 12; // full revolutions

  const points = React.useMemo(() => {
    const out = [];
    const steps = turns * 240;
    for (let i = 0; i <= steps; i++) {
      const t = (i / steps) * turns * 2 * Math.PI;
      const x = (R - r) * Math.cos(t) + d * Math.cos(((R - r) / r) * t);
      const y = (R - r) * Math.sin(t) - d * Math.sin(((R - r) / r) * t);
      out.push([x, y]);
    }
    return out;
  }, []);

  // Split into 2 strokes: outer reds, inner yellows — like the reference
  const path = (color, freq, off) => {
    const pts = points.filter((_, i) => (i + off) % freq === 0);
    return pts.map(([x, y], i) => `${i === 0 ? 'M' : 'L'} ${x.toFixed(2)} ${y.toFixed(2)}`).join(' ');
  };

  return (
    <svg viewBox="-120 -120 240 240" width={size} height={size} style={{ display: 'block' }}>
      <defs>
        <radialGradient id="spirobg" cx="50%" cy="50%" r="50%">
          <stop offset="0%" stopColor="rgba(254, 240, 138, 0.55)" />
          <stop offset="70%" stopColor="rgba(254, 240, 138, 0)" />
        </radialGradient>
      </defs>

      {/* warm halo */}
      <circle cx="0" cy="0" r="115" fill="url(#spirobg)" />

      {/* Outer ring with phase ticks */}
      <circle cx="0" cy="0" r="108" fill="none" stroke="#dc2626" strokeWidth="3" opacity="0.5" />
      <circle cx="0" cy="0" r="112" fill="none" stroke="#dc2626" strokeWidth="0.5" opacity="0.4" strokeDasharray="2 4" />

      {/* The pattern itself — two stroke passes for color variety */}
      <g style={{ mixBlendMode: blend }}>
        <path d={path('#dc2626', 1, 0)} fill="none" stroke="#dc2626" strokeWidth="0.6" opacity="0.55"
              strokeLinecap="round" strokeLinejoin="round"
              style={animate ? {
                strokeDasharray: 9000,
                strokeDashoffset: 9000,
                animation: 'spiro-draw 5s ease-out forwards',
              } : {}}/>
        <path d={path('#eab308', 2, 1)} fill="none" stroke="#eab308" strokeWidth="0.6" opacity="0.65"
              strokeLinecap="round" strokeLinejoin="round"
              style={animate ? {
                strokeDasharray: 9000,
                strokeDashoffset: 9000,
                animation: 'spiro-draw 5.6s ease-out 0.2s forwards',
              } : {}}/>
      </g>

      {/* Inner gear suggestion */}
      <circle cx="0" cy="0" r="38" fill="none" stroke="#eab308" strokeWidth="2" opacity="0.6" />
      <circle cx="0" cy="0" r="6" fill="#fbbf24" stroke="#92400e" strokeWidth="1" />
    </svg>
  );
};

// Hero block — concept on the left, spirograph art on the right
const LabsHero = ({ onJumpToLabs, onStartChat }) => (
  <section data-screen-label="Labs Hero" style={{
    position: 'relative',
    borderRadius: 20,
    overflow: 'hidden',
    background: 'linear-gradient(135deg, #fef3c7 0%, #fef2f2 50%, #fff7ed 100%)',
    border: '1px solid #fde68a',
    marginBottom: 26,
  }}>
    {/* Background spirograph pattern (subtle) */}
    <div style={{ position: 'absolute', right: -110, top: -110, opacity: 0.25, pointerEvents: 'none' }}>
      <Spirograph size={520} animate={false} />
    </div>

    <div style={{ position: 'relative', padding: '28px 32px', display: 'grid', gridTemplateColumns: 'minmax(0, 1fr) 360px', gap: 30, alignItems: 'center' }}>
      <div>
        <div className="row" style={{ gap: 6, marginBottom: 10 }}>
          <span className="badge" style={{ background: '#fde68a', color: '#78350f', fontSize: 11 }}>
            <I.Sparkle2 size={10} /> AI Labs · spin up a full AIOps stack in &lt;90s
          </span>
          <span className="badge badge-zinc mono">v0.9 · beta</span>
        </div>

        <h1 className="h1" style={{ margin: 0, fontSize: 32 }}>
          Trace a full <span style={{
            background: 'linear-gradient(135deg, #dc2626 0%, #eab308 100%)',
            WebkitBackgroundClip: 'text',
            WebkitTextFillColor: 'transparent',
          }}>AIOps loop</span> in one afternoon.
        </h1>

        <p style={{ margin: '10px 0 0', color: '#78350f', fontSize: 14.5, maxWidth: 540, lineHeight: 1.55 }} className="text-pretty">
          Sponsor labs ship pre-built Voltron Castles with a single model family, a curated KB,
          and a token grant. Pick one, agree (or decline) data-sharing, and you're running
          the full <strong>Recognize → Acquire → Organize/Reflect → Apply/Amplify</strong> loop the same day.
        </p>

        <div className="row" style={{ marginTop: 16, gap: 8 }}>
          <button className="btn btn-primary" onClick={onJumpToLabs} style={{ background: '#dc2626', borderColor: '#dc2626' }}>
            <I.Sparkles size={12} /> Browse 8 labs
          </button>
          <button className="btn" onClick={onStartChat}>
            <I.MessageCircle /> Ask which lab fits
          </button>
        </div>

        {/* Phase strip */}
        <div style={{ marginTop: 22, display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 8 }}>
          {RAOR_PHASES.map(p => <PhasePill key={p.id} phase={p} />)}
        </div>
      </div>

      {/* Right: spirograph */}
      <div style={{ display: 'grid', placeItems: 'center', position: 'relative' }}>
        <Spirograph size={320} animate={true} />
        {/* Floating phase labels around the ring */}
        {[
          { phase: RAOR_PHASES[0], x: '50%',  y: '8%',  ax: '-50%', ay: '0' },
          { phase: RAOR_PHASES[1], x: '92%',  y: '50%', ax: '-100%', ay: '-50%' },
          { phase: RAOR_PHASES[2], x: '50%',  y: '92%', ax: '-50%', ay: '-100%' },
          { phase: RAOR_PHASES[3], x: '8%',   y: '50%', ax: '0',     ay: '-50%' },
        ].map(({ phase, x, y, ax, ay }) => (
          <PhaseChip key={phase.id} phase={phase} style={{
            position: 'absolute', left: x, top: y,
            transform: `translate(${ax}, ${ay})`,
          }}/>
        ))}
      </div>
    </div>

    {/* draw animation keyframes */}
    <style>{`
      @keyframes spiro-draw {
        to { stroke-dashoffset: 0; }
      }
      @keyframes spiro-spin {
        from { transform: rotate(0deg); }
        to   { transform: rotate(360deg); }
      }
    `}</style>
  </section>
);

const PhasePill = ({ phase }) => (
  <div style={{
    padding: '8px 10px',
    background: 'white',
    border: `1px solid ${phase.color}33`,
    borderRadius: 10,
    borderLeft: `3px solid ${phase.color}`,
  }}>
    <div className="row" style={{ gap: 5, marginBottom: 2 }}>
      <span className="mono" style={{ fontSize: 9.5, color: phase.ink, fontWeight: 700, letterSpacing: '0.08em' }}>
        STEP {phase.step}
      </span>
    </div>
    <div style={{ fontSize: 11.5, fontWeight: 700, color: phase.ink, letterSpacing: '0.02em' }}>
      {phase.label}
    </div>
    <div style={{ fontSize: 11, color: 'var(--muted)', marginTop: 2 }}>{phase.blurb}</div>
  </div>
);

const PhaseChip = ({ phase, style }) => (
  <div style={{
    ...style,
    padding: '4px 9px',
    background: 'white',
    border: `1.5px solid ${phase.color}`,
    borderRadius: 999,
    fontSize: 10,
    fontWeight: 800,
    color: phase.ink,
    letterSpacing: '0.06em',
    boxShadow: `0 2px 8px ${phase.color}33`,
    fontFamily: 'JetBrains Mono, monospace',
    whiteSpace: 'nowrap',
  }}>
    {phase.label}
  </div>
);

Object.assign(window, { LabsHero, Spirograph, PhasePill, PhaseChip });
