// HeroGraphic.jsx — four hero graphic options, switchable via tweaks

function HeroGraphic({ variant }) {
  if (variant === 'monitor') return <MonitorGraphic />;
  if (variant === 'dotring') return <DotRingGraphic />;
  if (variant === 'flowloop') return <FlowLoopGraphic />;
  return <PlaceholderGraphic />;
}

// ── 1. Monitor with target + arrow + Align/Focus/Deliver/Learn pipeline ───────
function MonitorGraphic() {
  return (
    <div style={{ width: '100%', maxWidth: 560, aspectRatio: '5 / 4', position: 'relative' }}>
      <svg viewBox="0 0 500 400" style={{ width: '100%', height: '100%' }}>
        <defs>
          <linearGradient id="screenBg" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0" stopColor="#f6f9fc"/>
            <stop offset="1" stopColor="#e9eef7"/>
          </linearGradient>
        </defs>
        {/* Monitor frame */}
        <rect x="40" y="30" width="420" height="280" rx="14" fill="#fff" stroke="#dbe3ef" strokeWidth="2"/>
        <rect x="54" y="44" width="392" height="252" rx="6" fill="url(#screenBg)"/>
        {/* Stand */}
        <rect x="220" y="310" width="60" height="36" fill="#eef2f8" stroke="#dbe3ef" strokeWidth="2"/>
        <rect x="170" y="346" width="160" height="10" rx="4" fill="#dbe3ef"/>

        {/* Target (dartboard) */}
        <g transform="translate(130 140)">
          <circle r="46" fill="none" stroke="#cdd6e6" strokeWidth="2"/>
          <circle r="32" fill="none" stroke="#cdd6e6" strokeWidth="2"/>
          <circle r="16" fill="var(--brand-fern)" opacity=".18"/>
          <circle r="5" fill="var(--brand-fern)"/>
          {/* Arrow through target */}
          <line x1="-70" y1="-32" x2="18" y2="6" stroke="var(--brand-indigo)" strokeWidth="3" strokeLinecap="round"/>
          <polygon points="18,6 12,-2 22,-4" fill="var(--brand-indigo)"/>
          <polygon points="-70,-32 -78,-38 -72,-42" fill="var(--brand-indigo)" opacity=".5"/>
        </g>

        {/* Flow arrows with labels */}
        <g transform="translate(225 130)">
          {['Align', 'Focus', 'Deliver', 'Learn'].map((label, i) => (
            <g key={label} transform={`translate(0 ${i * 34})`}>
              <path d="M 0 0 L 170 0 L 185 12 L 170 24 L 0 24 L 15 12 Z" fill="var(--brand-indigo)"/>
              <text x="96" y="17" fill="#fff" fontSize="13" fontWeight="600" textAnchor="middle"
                    fontFamily="var(--font-body)">
                {label}
              </text>
            </g>
          ))}
        </g>
      </svg>
    </div>
  );
}

// ── 2. Animated dot ring (echoing logo) with phase labels ─────────────────────
function DotRingGraphic() {
  const [t, setT] = React.useState(0);
  React.useEffect(() => {
    let raf, start = performance.now();
    const loop = (now) => {
      setT((now - start) / 1000);
      raf = requestAnimationFrame(loop);
    };
    raf = requestAnimationFrame(loop);
    return () => cancelAnimationFrame(raf);
  }, []);

  const dots = 28;
  const radius = 130;
  const cx = 200, cy = 200;
  const labels = [
    { name: 'Align', angle: -Math.PI / 2, color: 'var(--brand-fern)' },
    { name: 'Focus', angle: 0, color: 'var(--brand-ocean)' },
    { name: 'Deliver', angle: Math.PI / 2, color: 'var(--brand-spruce)' },
    { name: 'Learn', angle: Math.PI, color: 'var(--brand-cta)' },
  ];

  return (
    <div style={{ width: '100%', maxWidth: 460, aspectRatio: '1', position: 'relative' }}>
      <svg viewBox="0 0 400 400" style={{ width: '100%', height: '100%' }}>
        {/* dots */}
        {Array.from({ length: dots }).map((_, i) => {
          const a = (i / dots) * Math.PI * 2 - Math.PI / 2;
          const phase = (i / dots + t * 0.15) % 1;
          const size = 4 + 5 * (0.5 + 0.5 * Math.sin(phase * Math.PI * 2));
          const greenish = i < dots / 2;
          const col = greenish ? 'var(--brand-spruce)' : 'var(--brand-fern)';
          return (
            <circle key={i}
              cx={cx + Math.cos(a) * radius}
              cy={cy + Math.sin(a) * radius}
              r={size}
              fill={col}
              opacity={0.55 + 0.45 * Math.sin(phase * Math.PI * 2)}
            />
          );
        })}
        {/* labels on cardinals */}
        {labels.map((l, i) => {
          const x = cx + Math.cos(l.angle) * (radius + 52);
          const y = cy + Math.sin(l.angle) * (radius + 52);
          return (
            <g key={l.name}>
              <circle cx={cx + Math.cos(l.angle) * (radius - 28)} cy={cy + Math.sin(l.angle) * (radius - 28)} r="6" fill={l.color}/>
              <text x={x} y={y + 4} textAnchor="middle" fontSize="15" fontWeight="700"
                    fill="var(--brand-indigo)" fontFamily="var(--font-heading)">
                {l.name}
              </text>
            </g>
          );
        })}
        {/* center */}
        <text x={cx} y={cy - 4} textAnchor="middle" fontSize="11" letterSpacing="2"
              fill="var(--fg-muted)" fontFamily="var(--font-body)" fontWeight="600">
          PPM4AGILE
        </text>
        <text x={cx} y={cy + 16} textAnchor="middle" fontSize="18" fontWeight="700"
              fill="var(--brand-indigo)" fontFamily="var(--font-heading)">
          operating system
        </text>
      </svg>
    </div>
  );
}

// ── 3. Horizontal flow loop: 4 stages connected with curved return ────────────
function FlowLoopGraphic() {
  const stages = [
    { name: 'Align',   sub: 'Portfolio',  color: 'var(--brand-fern)' },
    { name: 'Focus',   sub: 'Planning',   color: 'var(--brand-ocean)' },
    { name: 'Deliver', sub: 'Execution',  color: 'var(--brand-spruce)' },
    { name: 'Learn',   sub: 'Feedback',   color: 'var(--brand-cta)' },
  ];
  return (
    <div style={{ width: '100%', maxWidth: 520 }}>
      <svg viewBox="0 0 520 360" style={{ width: '100%' }}>
        {/* return arc */}
        <path d="M 460 170 C 500 170, 500 300, 440 300 L 80 300 C 20 300, 20 170, 60 170"
              fill="none" stroke="var(--fg-muted)" strokeWidth="2" strokeDasharray="4 5" opacity=".5"/>
        <polygon points="60,170 68,164 68,176" fill="var(--fg-muted)" opacity=".7"/>

        {stages.map((s, i) => {
          const x = 40 + i * 120;
          return (
            <g key={s.name} transform={`translate(${x} 100)`}>
              <rect width="100" height="100" rx="16" fill="#fff" stroke={s.color} strokeWidth="2"/>
              <circle cx="50" cy="40" r="12" fill={s.color} opacity=".18"/>
              <circle cx="50" cy="40" r="5" fill={s.color}/>
              <text x="50" y="72" textAnchor="middle" fontSize="14" fontWeight="700"
                    fill="var(--brand-indigo)" fontFamily="var(--font-heading)">{s.name}</text>
              <text x="50" y="88" textAnchor="middle" fontSize="10"
                    fill="var(--fg-muted)" fontFamily="var(--font-body)">{s.sub}</text>
              {i < 3 && (
                <g>
                  <line x1="100" y1="50" x2="118" y2="50" stroke="var(--brand-indigo)" strokeWidth="2"/>
                  <polygon points="118,50 112,46 112,54" fill="var(--brand-indigo)"/>
                </g>
              )}
            </g>
          );
        })}
      </svg>
    </div>
  );
}

// ── 4. Placeholder ─────────────────────────────────────────────────────────────
function PlaceholderGraphic() {
  return (
    <div style={{
      width: '100%',
      maxWidth: 520,
      aspectRatio: '5 / 4',
      background: 'repeating-linear-gradient(-45deg, rgba(27,38,79,.04) 0 12px, rgba(27,38,79,.08) 12px 13px)',
      border: '1px dashed rgba(27,38,79,.25)',
      borderRadius: 16,
      display: 'grid',
      placeItems: 'center',
      fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
      fontSize: 12,
      color: 'var(--fg-muted)',
      letterSpacing: '0.08em',
      textTransform: 'uppercase',
    }}>
      [ hero illustration ]
    </div>
  );
}

window.HeroGraphic = HeroGraphic;
