/* global React */
// Five hero diagram alternatives, all sized to the same 5:4 frame so they're
// drop-in swappable from the Tweaks panel. Each is rooted in Latus's visual
// vocabulary (Foundation diagram, hairline geometry, cool sculptural imagery).

const heroDiaStyles = {
  frame: {
    position: 'relative',
    width: '100%', aspectRatio: '1/1', maxWidth: 600, marginLeft: 'auto'
  },
  caption: {
    marginTop: 'var(--s-4)', textAlign: 'right',
    fontSize: 12, color: 'var(--fg-3)',
    letterSpacing: '0.08em', textTransform: 'uppercase'
  },
  svg: { width: '100%', height: '100%', display: 'block' }
};

/* ---- 1 · Architectural (the original ambient flow) ---------------------- */
function DiagramArchitectural() {
  return (
    <div style={heroDiaStyles.frame} aria-hidden="true">
      <svg viewBox="0 0 500 400" style={heroDiaStyles.svg}>
        <defs>
          <pattern id="ha-grid" width="20" height="20" patternUnits="userSpaceOnUse">
            <path d="M 20 0 L 0 0 0 20" fill="none" stroke="rgba(3,31,88,0.06)" strokeWidth="1" />
          </pattern>
          <linearGradient id="ha-fade" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor="white" stopOpacity="0" />
            <stop offset="0.15" stopColor="white" stopOpacity="1" />
            <stop offset="0.85" stopColor="white" stopOpacity="1" />
            <stop offset="1" stopColor="white" stopOpacity="0" />
          </linearGradient>
          <mask id="ha-mask"><rect width="500" height="400" fill="url(#ha-fade)" /></mask>
        </defs>
        <rect width="500" height="400" fill="url(#ha-grid)" />
        <g stroke="rgba(3,31,88,0.18)" fill="none" strokeWidth="1.25">
          <rect x="60" y="60" width="380" height="280" />
          <rect x="60" y="60" width="380" height="160" />
          <rect x="60" y="60" width="380" height="60" />
          <line x1="60" y1="220" x2="440" y2="220" />
        </g>
        <g mask="url(#ha-mask)">
          <line x1="0" y1="110" x2="500" y2="110" stroke="rgba(3,31,88,0.22)" strokeWidth="1" />
          <line x1="0" y1="200" x2="500" y2="200" stroke="rgba(3,31,88,0.22)" strokeWidth="1" />
          <line x1="0" y1="290" x2="500" y2="290" stroke="rgba(3,31,88,0.22)" strokeWidth="1" />
        </g>
        <g fill="var(--latus-off-white)" stroke="var(--latus-dark-blue)" strokeWidth="1.25">
          <rect x="115" y="190" width="20" height="20" />
          <rect x="240" y="190" width="20" height="20" />
          <rect x="365" y="190" width="20" height="20" />
        </g>
        <circle r="4" fill="var(--latus-indigo)">
          <animateMotion dur="9s" repeatCount="indefinite" path="M 0 110 L 500 110" />
        </circle>
        <circle r="3" fill="var(--latus-indigo)" opacity="0.7">
          <animateMotion dur="11s" begin="-3s" repeatCount="indefinite" path="M 0 200 L 500 200" />
        </circle>
        <circle r="3" fill="var(--latus-indigo)" opacity="0.5">
          <animateMotion dur="13s" begin="-6s" repeatCount="indefinite" path="M 0 290 L 500 290" />
        </circle>
      </svg>
      <div style={heroDiaStyles.caption}>Foundation · flow · production</div>
    </div>);

}

/* ---- 2 · Foundation (literal brand mirror — the house) ----------------- */
function DiagramFoundation() {
  // Stroke-draw on entry by animating stroke-dashoffset
  return (
    <div style={heroDiaStyles.frame} aria-hidden="true">
      <svg viewBox="0 0 500 400" style={heroDiaStyles.svg}>
        <defs>
          <pattern id="hf-grid" width="20" height="20" patternUnits="userSpaceOnUse">
            <circle cx="10" cy="10" r="0.6" fill="rgba(3,31,88,0.18)" />
          </pattern>
          <style>{`
            .hf-draw { stroke-dasharray: 1400; stroke-dashoffset: 1400; animation: hf-draw 1400ms 200ms var(--ease-out) forwards; }
            .hf-draw-2 { animation-delay: 700ms; }
            .hf-draw-3 { animation-delay: 1100ms; }
            .hf-fade { opacity: 0; animation: hf-fade 600ms 1600ms var(--ease-out) forwards; }
            @keyframes hf-draw { to { stroke-dashoffset: 0; } }
            @keyframes hf-fade { to { opacity: 1; } }
            @media (prefers-reduced-motion: reduce) {
              .hf-draw, .hf-draw-2, .hf-draw-3, .hf-fade {
                stroke-dashoffset: 0 !important; opacity: 1 !important; animation: none !important;
              }
            }
          `}</style>
        </defs>
        <rect width="500" height="400" fill="url(#hf-grid)" />

        {/* Roof — triangle (no bottom, sits on the rectangle below) */}
        <polyline points="60,180 250,40 440,180"
        fill="none" stroke="var(--latus-indigo)" strokeWidth="1.5"
        className="hf-draw" />

        {/* Foundation (mid) rectangle */}
        <rect x="60" y="180" width="380" height="140"
        fill="none" stroke="var(--latus-indigo)" strokeWidth="1.5"
        className="hf-draw hf-draw-2" />

        {/* Strategy (base) rectangle */}
        <rect x="60" y="335" width="380" height="50"
        fill="none" stroke="var(--latus-indigo)" strokeWidth="1.5"
        className="hf-draw hf-draw-3" />

        {/* Labels */}
        <g className="hf-fade" fontFamily="var(--font-sans)"
        fill="var(--latus-dark-blue)" fontWeight="500" fontSize="14"
        letterSpacing="0.02em" textAnchor="middle">
          <text x="250" y="135">Application</text>
          <text x="250" y="255">AI &amp; Data</text>
          <text x="250" y="365">Platform</text>
        </g>
      </svg>
      <div style={heroDiaStyles.caption}>Application · AI &amp; Data · Platform</div>
    </div>);

}

/* ---- 3 · Imagery (full-bleed brand photo) ------------------------------ */
function DiagramImagery() {
  return (
    <div style={{ ...heroDiaStyles.frame, position: 'relative' }} aria-hidden="true">
      <div style={{
        position: 'relative', width: '100%', aspectRatio: '5/4',
        borderRadius: 'var(--r-3)', overflow: 'hidden',
        border: 'var(--border-hair)'
      }}>
        <img src="assets/imagery/plenio-curves.jpg" alt=""
        style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
        {/* Foundation glyph overlay, bottom-left */}
        <div style={{
          position: 'absolute', left: 16, bottom: 16,
          background: 'rgba(250,250,250,0.92)', backdropFilter: 'blur(6px)',
          padding: '8px 10px', display: 'flex', alignItems: 'center', gap: 10
        }}>
          <svg width="32" height="20" viewBox="0 0 36 22" aria-hidden="true">
            <g fill="none" stroke="var(--latus-dark-blue)" strokeWidth="1.25">
              <polyline points="4,12 18,3 32,12" />
              <rect x="4" y="12" width="28" height="6" />
              <rect x="4" y="18" width="28" height="2" />
            </g>
          </svg>
          <span style={{
            fontSize: 11, fontWeight: 500, color: 'var(--latus-dark-blue)',
            letterSpacing: '0.08em', textTransform: 'uppercase'
          }}>Latus</span>
        </div>
      </div>
      <div style={heroDiaStyles.caption}>Editorial · sculptural · cool</div>
    </div>);

}

/* ---- 4 · Wireframe globe (NL ↔ PT) ------------------------------------ */
function DiagramGlobe() {
  // Sparse wireframe; two indigo dots for Amsterdam + Ericeira, arc between.
  // Math: projected to 2D with lat→y, lon→x, simple sphere projection.
  const cx = 250,cy = 210,R = 140;
  const project = (lat, lon) => ({
    x: cx + R * Math.cos(lat * Math.PI / 180) * Math.sin(lon * Math.PI / 180),
    y: cy - R * Math.sin(lat * Math.PI / 180)
  });
  const ams = project(52.37, 4.90);
  const eri = project(38.97, -9.42);

  // Build latitude lines (ellipses with varying ry)
  const lats = [-60, -30, 0, 30, 60];
  const lons = [-90, -60, -30, 0, 30, 60, 90];

  return (
    <div style={heroDiaStyles.frame} aria-hidden="true">
      <svg viewBox="0 0 500 400" style={heroDiaStyles.svg}>
        <defs>
          <radialGradient id="hg-fade" cx="50%" cy="52%" r="50%">
            <stop offset="0.7" stopColor="white" stopOpacity="1" />
            <stop offset="1" stopColor="white" stopOpacity="0" />
          </radialGradient>
          <mask id="hg-mask"><circle cx={cx} cy={cy} r={R + 4} fill="url(#hg-fade)" /></mask>
          <style>{`
            .hg-rot { transform-origin: ${cx}px ${cy}px; animation: hg-spin 60s linear infinite; }
            @keyframes hg-spin { to { transform: rotate(360deg); } }
            @media (prefers-reduced-motion: reduce) { .hg-rot { animation: none; } }
          `}</style>
        </defs>

        {/* Outer rule + corner ticks */}
        <g stroke="rgba(3,31,88,0.16)" fill="none" strokeWidth="1">
          <line x1="40" y1="40" x2="60" y2="40" />
          <line x1="40" y1="40" x2="40" y2="60" />
          <line x1="460" y1="40" x2="440" y2="40" />
          <line x1="460" y1="40" x2="460" y2="60" />
          <line x1="40" y1="360" x2="60" y2="360" />
          <line x1="40" y1="360" x2="40" y2="340" />
          <line x1="460" y1="360" x2="440" y2="360" />
          <line x1="460" y1="360" x2="460" y2="340" />
        </g>

        <g mask="url(#hg-mask)" className="hg-rot">
          {/* equator + outline */}
          <circle cx={cx} cy={cy} r={R} fill="none" stroke="rgba(3,31,88,0.35)" strokeWidth="1.25" />
          <ellipse cx={cx} cy={cy} rx={R} ry={3} fill="none" stroke="rgba(3,31,88,0.35)" strokeWidth="1" />
          {/* latitudes */}
          {lats.map((lat) => {
            const y = cy - R * Math.sin(lat * Math.PI / 180);
            const rx = R * Math.cos(lat * Math.PI / 180);
            return <ellipse key={lat} cx={cx} cy={y} rx={rx} ry={3} fill="none" stroke="rgba(3,31,88,0.22)" strokeWidth="1" />;
          })}
          {/* longitudes — rotated ellipses */}
          {lons.map((lon) =>
          <ellipse key={lon} cx={cx} cy={cy} rx={Math.abs(R * Math.sin(lon * Math.PI / 180))} ry={R}
          fill="none" stroke="rgba(3,31,88,0.22)" strokeWidth="1" />
          )}
        </g>

        {/* Markers (do not rotate — fixed at NL/PT) */}
        <g>
          {/* Connecting arc */}
          <path d={`M ${eri.x} ${eri.y} Q ${(eri.x + ams.x) / 2} ${Math.min(eri.y, ams.y) - 36} ${ams.x} ${ams.y}`}
          fill="none" stroke="var(--latus-indigo)" strokeWidth="1.25" strokeDasharray="4 4" />
          <circle cx={ams.x} cy={ams.y} r="4" fill="var(--latus-indigo)" />
          <circle cx={eri.x} cy={eri.y} r="4" fill="var(--latus-indigo)" />
          {/* Callouts */}
          <g fontFamily="var(--font-sans)" fontSize="10.5" fontWeight="500"
          letterSpacing="0.08em" fill="var(--latus-dark-blue)">
            <line x1={ams.x + 6} y1={ams.y} x2={ams.x + 36} y2={ams.y - 22} stroke="var(--latus-dark-blue)" strokeWidth="1" />
            <text x={ams.x + 40} y={ams.y - 22} dominantBaseline="middle">AMSTERDAM</text>
            <text x={ams.x + 40} y={ams.y - 8} fontSize="9" fill="var(--fg-3)" letterSpacing="0">52.37°N · 4.90°E</text>

            <line x1={eri.x - 6} y1={eri.y} x2={eri.x - 36} y2={eri.y + 22} stroke="var(--latus-dark-blue)" strokeWidth="1" />
            <text x={eri.x - 40} y={eri.y + 22} textAnchor="end" dominantBaseline="middle">ERICEIRA</text>
            <text x={eri.x - 40} y={eri.y + 36} textAnchor="end" fontSize="9" fill="var(--fg-3)" letterSpacing="0">38.97°N · 9.42°W</text>
          </g>
        </g>
      </svg>
      <div style={heroDiaStyles.caption}>Remote · onsite · arc</div>
    </div>);

}

/* ---- 5 · Typographic (drafting label / identity plate) ---------------- */
function DiagramTypographic() {
  return (
    <div style={{ ...heroDiaStyles.frame }} aria-hidden="true">
      <div style={{
        position: 'relative', width: '100%', aspectRatio: '5/4',
        background: 'var(--bg-1)',
        border: 'var(--border-hair)',
        padding: 'clamp(20px, 4%, 36px)',
        display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
        overflow: 'hidden'
      }}>
        {/* Corner ticks */}
        {[
        { top: 8, left: 8, h: '12px 0 0 12px' },
        { top: 8, right: 8, h: '0 12px 0 0' },
        { bottom: 8, left: 8, h: '0 0 0 12px' },
        { bottom: 8, right: 8, h: '0 0 12px 0' }].
        map((s, i) =>
        <span key={i} style={{
          position: 'absolute',
          top: s.top, left: s.left, right: s.right, bottom: s.bottom,
          width: 14, height: 14,
          borderTop: s.top !== undefined ? '1px solid var(--latus-dark-blue)' : 'none',
          borderBottom: s.bottom !== undefined ? '1px solid var(--latus-dark-blue)' : 'none',
          borderLeft: s.left !== undefined ? '1px solid var(--latus-dark-blue)' : 'none',
          borderRight: s.right !== undefined ? '1px solid var(--latus-dark-blue)' : 'none'
        }} />
        )}

        {/* Top metadata row */}
        <div style={{
          display: 'flex', justifyContent: 'space-between',
          fontSize: 10, fontWeight: 500, letterSpacing: '0.12em',
          color: 'var(--fg-3)', textTransform: 'uppercase'
        }}>
          <span>ID · DVM</span>
          <span>2026</span>
        </div>

        {/* Stacked wordmark */}
        <div style={{
          display: 'flex', flexDirection: 'column',
          textAlign: 'right',
          fontWeight: 500,
          color: 'var(--latus-dark-blue)',
          lineHeight: 0.95,
          letterSpacing: 'var(--tracking-tight)'
        }}>
          <span style={{ fontSize: 'clamp(22px, 3.8vw, 38px)' }}>Full-stack</span>
          <span style={{ fontSize: 'clamp(40px, 7vw, 84px)', color: 'var(--latus-indigo)' }}>AI</span>
          <span style={{ fontSize: 'clamp(22px, 3.8vw, 38px)' }}>engineer</span>
        </div>

        {/* Bottom metadata row */}
        <div style={{
          display: 'flex', justifyContent: 'space-between',
          fontSize: 10, fontWeight: 500, letterSpacing: '0.12em',
          color: 'var(--fg-3)', textTransform: 'uppercase',
          borderTop: 'var(--border-hair)', paddingTop: 8
        }}>
          <span>NL · PT</span>
          <span>Independent</span>
        </div>
      </div>
      <div style={heroDiaStyles.caption}>Wordmark · drafting plate</div>
    </div>);

}

/* ---- 6 · Latus (the mark, animated through its own geometry) -------- */
function DiagramLatus() {
  // The Latus icon is a square + crossed diagonals + parabolic arc.
  // Reading it geometrically: one diagonal is the axis of symmetry, the other
  // is the latus rectum (the chord through the focus), and the arc is the
  // parabola itself. We animate the mark's actual geometry:
  //   • the square + axis + arc draw in (calm)
  //   • the latus rectum draws last, in indigo — it's the active element
  //   • a dot oscillates along the latus rectum through the focus
  //   • two dots travel along the parabolic arc, staggered
  return (
    <div style={heroDiaStyles.frame} aria-hidden="true">
      <svg viewBox="0 0 500 500" preserveAspectRatio="xMidYMid meet" style={heroDiaStyles.svg}>
        <defs>
          <style>{`
            .lt-draw {
              stroke-dasharray: 2200;
              stroke-dashoffset: 2200;
              animation: lt-draw 900ms var(--ease-out) forwards;
            }
            .lt-fade {
              opacity: 0;
              animation: lt-fade 500ms var(--ease-out) forwards;
            }
            @keyframes lt-draw { to { stroke-dashoffset: 0; } }
            @keyframes lt-fade { to { opacity: 1; } }
            @media (prefers-reduced-motion: reduce) {
              .lt-draw { stroke-dashoffset: 0; animation: none; }
              .lt-fade { opacity: 1; animation: none; }
              .lt-motion { display: none; }
            }
          `}</style>
          <pattern id="lt-grid" width="25" height="25" patternUnits="userSpaceOnUse">
            <circle cx="12.5" cy="12.5" r="0.7" fill="rgba(3,31,88,0.14)" />
          </pattern>
        </defs>

        <rect width="500" height="500" fill="url(#lt-grid)" />

        {/* Parabolic arc (paint underneath so the frame stays visible) */}
        <path d="M 10 10 A 480 480 0 0 0 490 490" fill="none"
        stroke="var(--latus-dark-blue)" strokeWidth="1.25"
        className="lt-draw" style={{ animationDelay: '1100ms' }} />

        {/* Square frame — drawn after the arc so it always sits on top */}
        <rect x="10" y="10" width="480" height="480" fill="none"
        stroke="var(--latus-dark-blue)" strokeWidth="1.25"
        className="lt-draw" style={{ animationDelay: '100ms' }} />

        {/* Axis (NW-SE diagonal) */}
        <line x1="10" y1="10" x2="490" y2="490"
        stroke="var(--latus-dark-blue)" strokeWidth="1.25"
        className="lt-draw" style={{ animationDelay: '650ms' }} />

        {/* Latus rectum (NE-SW diagonal) */}
        <line x1="490" y1="10" x2="10" y2="490"
        stroke="var(--latus-dark-blue)" strokeWidth="1.25"
        className="lt-draw" style={{ animationDelay: '1500ms' }} />

        {/* Motion */}
        <g className="lt-fade lt-motion" style={{ animationDelay: '2000ms' }}>
          {/* Dot oscillating along the latus rectum, easing through the focus */}
          <circle r="5" fill="var(--latus-indigo)">
            <animateMotion dur="4.5s" repeatCount="indefinite"
            calcMode="spline" keyTimes="0;0.5;1"
            keySplines="0.45 0 0.55 1; 0.45 0 0.55 1"
            path="M 490 10 L 10 490 L 490 10" />
          </circle>
          {/* Two dots travelling along the arc, staggered */}
          <circle r="4" fill="var(--latus-indigo)" opacity="0.85">
            <animateMotion dur="7s" repeatCount="indefinite"
            path="M 10 10 A 480 480 0 0 0 490 490" />
          </circle>
          <circle r="3.5" fill="var(--latus-indigo)" opacity="0.55">
            <animateMotion dur="7s" begin="-2.5s" repeatCount="indefinite"
            path="M 10 10 A 480 480 0 0 0 490 490" />
          </circle>
        </g>

      </svg>
    </div>);

}

const HERO_DIAGRAMS = {
  latus: DiagramLatus,
  architectural: DiagramArchitectural,
  foundation: DiagramFoundation,
  imagery: DiagramImagery,
  globe: DiagramGlobe,
  typographic: DiagramTypographic
};

const HERO_DIAGRAM_OPTIONS = ['latus', 'architectural', 'foundation', 'imagery', 'globe', 'typographic'];

Object.assign(window, { HERO_DIAGRAMS, HERO_DIAGRAM_OPTIONS });