// ============ CITRUS HERO ============
// Big featured citrus character. Sits on a glowing pedestal,
// floating chips orbit around it, a speech bubble greets you,
// subtle idle animation. The face of the brand.
//
// Animations layered on top of the original idle motion:
//   A · cycling speech bubble  (React state, every ~5s)
//   B · sequential visor pulse (CSS, left → middle → right)
//   C · sequential chip ping   (CSS, one chip outline-pulses every ~4s)
//   D · cursor-aware head tilt (mousemove on stage, max ±5°)

const { useState: useCState, useEffect: useCEffect, useRef: useCRef } = React;

const CITRUS_PALETTE = {
  skin: "#E85D1A",
  skinDark: "#B84410",
  accent: "#FFCA7D",
  visor: "#1A120B",
};

// (A) Cycling speech bubble messages.
const HERO_MESSAGES = [
  ["Citrus",  "Hey — what do you want me\nto take off your plate?"],
  ["ORI",     "Inbox is piling up — want me\nto jump in and reply?"],
  ["SOL",     "It's almost Monday — should I\nwrite this week's brief?"],
  ["VEX",     "Three leads have gone cold.\nWant me to follow up?"],
  ["INK",     "New contract just landed —\nshould I read it first?"],
];
const MSG_INTERVAL = 5000;
const MSG_FADE     = 280;

function CitrusHero() {
  // (A) message cycling
  const [msgIndex, setMsgIndex] = useCState(0);
  const [fading, setFading]     = useCState(false);

  useCEffect(() => {
    const id = setInterval(() => {
      setFading(true);
      setTimeout(() => {
        setMsgIndex(i => (i + 1) % HERO_MESSAGES.length);
        setFading(false);
      }, MSG_FADE);
    }, MSG_INTERVAL);
    return () => clearInterval(id);
  }, []);

  // (D) cursor head tilt
  const stageRef               = useCRef(null);
  const [tracking, setTracking] = useCState(false);
  const [headRot, setHeadRot]   = useCState(0);

  useCEffect(() => {
    const stage = stageRef.current;
    if (!stage) return;

    const onMove = (e) => {
      const r = stage.getBoundingClientRect();
      const dx = e.clientX - (r.left + r.width / 2);
      const t = Math.max(-1, Math.min(1, dx / (r.width / 2 || 1)));
      setHeadRot(+(t * 5).toFixed(2));
      if (!tracking) setTracking(true);
    };
    const onLeave = () => setTracking(false);

    stage.addEventListener("mousemove", onMove);
    stage.addEventListener("mouseleave", onLeave);
    return () => {
      stage.removeEventListener("mousemove", onMove);
      stage.removeEventListener("mouseleave", onLeave);
    };
  }, [tracking]);

  const [tag, msg] = HERO_MESSAGES[msgIndex];

  return (
    <div
      className={`ch-stage ${tracking ? "is-tracking" : ""}`}
      ref={stageRef}
      style={{ "--head-rot": `${headRot}deg` }}
      aria-hidden="false"
      aria-label="Citrus, the AI agent"
    >
      <div className="ch-glow" />
      <div className="ch-grid-bg" />

      {/* (A) speech bubble — content cycles every ~5s with a fade transition */}
      <div className="ch-bubble">
        <span className={`ch-bubble-tag ${fading ? "is-fading" : ""}`}>{tag}</span>
        <span className={`ch-bubble-msg ${fading ? "is-fading" : ""}`}>
          {msg.split("\n").map((line, i, arr) => (
            <React.Fragment key={i}>
              {line}
              {i < arr.length - 1 ? <br /> : null}
            </React.Fragment>
          ))}
        </span>
        <span className="ch-bubble-tip" />
      </div>

      {/* floating tool chips — chip-ping CSS pulses one of them in turn (C) */}
      <div className="ch-chip ch-chip-a"><ChipWhatsApp /><span>WhatsApp</span></div>
      <div className="ch-chip ch-chip-b"><ChipStripe   /><span>Stripe</span></div>
      <div className="ch-chip ch-chip-c"><ChipSheets   /><span>Sheets</span></div>
      <div className="ch-chip ch-chip-d"><ChipGmail    /><span>Gmail</span></div>

      {/* the character */}
      <div className="ch-character">
        <div className="ch-shadow" />
        <div className="ch-body"><CitrusFigure /></div>

        {/* status pill above head */}
        <div className="ch-status">
          <span className="ch-status-dot" />
          on shift
        </div>
      </div>

      {/* sparkles */}
      <span className="ch-spark ch-spark-1">✦</span>
      <span className="ch-spark ch-spark-2">✦</span>
      <span className="ch-spark ch-spark-3">✦</span>
    </div>
  );
}

// ===== Big custom citrus figure (more detail than the small avatar) =====
function CitrusFigure() {
  return (
    <svg viewBox="0 0 320 380" className="ch-fig" preserveAspectRatio="xMidYMax meet">
      <defs>
        <radialGradient id="ch-head" cx="36%" cy="30%" r="80%">
          <stop offset="0%" stopColor="#FFD9A8" />
          <stop offset="40%" stopColor="#FFA85C" />
          <stop offset="80%" stopColor="#E85D1A" />
          <stop offset="100%" stopColor="#B84410" />
        </radialGradient>
        <radialGradient id="ch-cheek" cx="50%" cy="50%" r="50%">
          <stop offset="0%" stopColor="#FF7A2D" stopOpacity="0.6" />
          <stop offset="100%" stopColor="#FF7A2D" stopOpacity="0" />
        </radialGradient>
        <linearGradient id="ch-body" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" stopColor="#1F1610" />
          <stop offset="100%" stopColor="#0E0905" />
        </linearGradient>
        <linearGradient id="ch-visor" x1="0%" y1="0%" x2="100%" y2="100%">
          <stop offset="0%" stopColor="#0E0905" />
          <stop offset="50%" stopColor="#2A1A0E" />
          <stop offset="100%" stopColor="#0E0905" />
        </linearGradient>
        <radialGradient id="ch-visor-shine" cx="30%" cy="30%" r="40%">
          <stop offset="0%" stopColor="#FFCA7D" stopOpacity="0.7" />
          <stop offset="100%" stopColor="#FFCA7D" stopOpacity="0" />
        </radialGradient>
        <filter id="ch-soft" x="-20%" y="-20%" width="140%" height="140%">
          <feGaussianBlur stdDeviation="0.5" />
        </filter>
      </defs>

      {/* body */}
      <g className="ch-fig-body">
        {/* arms */}
        <ellipse cx="78" cy="280" rx="22" ry="32" fill="url(#ch-body)" transform="rotate(-12 78 280)" />
        <ellipse cx="242" cy="280" rx="22" ry="32" fill="url(#ch-body)" transform="rotate(12 242 280)" />
        {/* hands */}
        <circle cx="64" cy="306" r="14" fill="#E85D1A" />
        <circle cx="60" cy="302" r="5" fill="#FFB37A" opacity="0.6" />
        <circle cx="256" cy="306" r="14" fill="#E85D1A" />
        <circle cx="252" cy="302" r="5" fill="#FFB37A" opacity="0.6" />

        {/* torso */}
        <path d="M 90 260 Q 90 220 130 220 L 190 220 Q 230 220 230 260 L 230 360 Q 230 376 214 376 L 106 376 Q 90 376 90 360 Z"
              fill="url(#ch-body)" />
        {/* torso highlight */}
        <path d="M 110 232 Q 110 226 116 226 L 130 226"
              stroke="rgba(255,202,125,0.18)" strokeWidth="2" fill="none" strokeLinecap="round" />
        {/* badge on chest */}
        <circle cx="160" cy="295" r="14" fill="#1A120B" stroke="#FFCA7D" strokeWidth="1.5" />
        <text x="160" y="301" textAnchor="middle" fill="#FFCA7D"
              style={{ fontFamily: "var(--display-font, serif)", fontSize: 14, fontWeight: 700 }}>◍</text>
      </g>

      {/* neck */}
      <rect x="148" y="200" width="24" height="28" rx="4" fill="#1F1610" />

      {/* head */}
      <g className="ch-fig-head">
        {/* citrus segments hint behind head */}
        <circle cx="160" cy="120" r="92" fill="url(#ch-head)" />
        {/* segment lines for citrus feel */}
        <g stroke="#B84410" strokeWidth="0.8" opacity="0.35" fill="none">
          <path d="M 160 30 Q 130 120 160 210" />
          <path d="M 160 30 Q 190 120 160 210" />
          <path d="M 70 120 Q 160 90 250 120" />
          <path d="M 80 90 Q 160 105 240 90" />
          <path d="M 80 150 Q 160 135 240 150" />
        </g>
        {/* leaf on top */}
        <path d="M 160 28 Q 175 8 195 14 Q 192 30 175 36 Q 165 36 160 30 Z" fill="#3F8C3A" />
        <path d="M 175 18 Q 182 22 188 22" stroke="#2A6228" strokeWidth="1" fill="none" />
        {/* stem */}
        <rect x="156" y="28" width="6" height="10" rx="2" fill="#3F2814" />

        {/* cheeks */}
        <ellipse cx="100" cy="148" rx="22" ry="14" fill="url(#ch-cheek)" />
        <ellipse cx="220" cy="148" rx="22" ry="14" fill="url(#ch-cheek)" />

        {/* visor band */}
        <rect x="78" y="100" width="164" height="46" rx="22" fill="url(#ch-visor)" />
        {/* visor inner highlight */}
        <rect x="84" y="106" width="152" height="34" rx="17" fill="none"
              stroke="rgba(255,202,125,0.22)" strokeWidth="1" />

        {/* (B) eye glints — wrapped in <g className="ch-eye-*"> so we can
              animate the pair (outer + inner) together as a unit. */}
        <g className="ch-eye ch-eye-l">
          <circle cx="118" cy="123" r="4"   fill="#FFE7B5" />
          <circle cx="118" cy="123" r="1.8" fill="#FFFFFF" />
        </g>

        {/* central glyph also pulses — middle of the scan sweep */}
        <text x="160" y="129" textAnchor="middle" fill="#FFCA7D" className="ch-eye ch-eye-m"
              style={{ fontFamily: "var(--display-font, serif)", fontSize: 14, fontWeight: 700, letterSpacing: "0.18em" }}>
          ◍
        </text>

        <g className="ch-eye ch-eye-r">
          <circle cx="202" cy="123" r="4"   fill="#FFE7B5" />
          <circle cx="202" cy="123" r="1.8" fill="#FFFFFF" />
        </g>

        {/* visor shine */}
        <rect x="78" y="100" width="164" height="46" rx="22" fill="url(#ch-visor-shine)" />

        {/* mouth — small smile */}
        <path d="M 144 178 Q 160 188 176 178" stroke="#7A2A0A" strokeWidth="2.4" strokeLinecap="round" fill="none" />
      </g>
    </svg>
  );
}

// ===== Tool chip icons =====
function ChipWhatsApp() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
      <circle cx="7" cy="7" r="6" fill="#25D366" />
      <path d="M4.5 9.5 L 5 7.5 Q 4.3 6.5 4.6 5.4 Q 5.1 4 6.6 4 Q 8.2 4 8.6 5.6 Q 8.9 6.8 8 8 Q 7 8.7 5.6 8.5 L 4.5 9.5 Z"
            fill="#fff" />
    </svg>
  );
}
function ChipStripe() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
      <rect width="14" height="14" rx="3" fill="#635BFF" />
      <path d="M5.4 5.5c0-.4.3-.6.8-.6.7 0 1.6.2 2.3.6V3.7c-.7-.3-1.5-.4-2.3-.4-1.9 0-3.1 1-3.1 2.6 0 2.5 3.5 2.1 3.5 3.2 0 .4-.4.6-.9.6-.8 0-1.8-.3-2.6-.7v1.9c.9.4 1.7.5 2.6.5 1.9 0 3.2-.9 3.2-2.6 0-2.7-3.5-2.2-3.5-3.3z"
            fill="#fff" />
    </svg>
  );
}
function ChipSheets() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
      <rect width="14" height="14" rx="2" fill="#0F9D58" />
      <rect x="3" y="4" width="8" height="6.5" rx="0.5" fill="#fff" />
      <line x1="3" y1="6" x2="11" y2="6" stroke="#0F9D58" strokeWidth="0.6" />
      <line x1="3" y1="8" x2="11" y2="8" stroke="#0F9D58" strokeWidth="0.6" />
      <line x1="6" y1="4" x2="6" y2="10.5" stroke="#0F9D58" strokeWidth="0.6" />
      <line x1="8.5" y1="4" x2="8.5" y2="10.5" stroke="#0F9D58" strokeWidth="0.6" />
    </svg>
  );
}
function ChipGmail() {
  return (
    <svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
      <rect width="14" height="14" rx="2" fill="#fff" stroke="rgba(0,0,0,0.06)" />
      <path d="M2 4.5 L 7 8 L 12 4.5 V 10 H 2 Z" fill="#EA4335" />
      <path d="M2 4.5 L 7 8 L 12 4.5" stroke="#C5221F" strokeWidth="0.4" fill="none" />
    </svg>
  );
}

window.CitrusHero = CitrusHero;
