// ConceptE_Agent — Beat 1. A single labeled "Extraction Agent" CARD that
// visibly processes one document. No character, no cube-with-a-face.
//
// Layout (world coords, anchor at AX/AY = ~(500, 500) in the camera's tight frame):
//
//   ┌── incoming docs (left) ──┐        ┌── Extraction Agent card ──┐        ┌── extracted fields ──┐
//   │   stacked paper           │  ───▶  │   [scan line sweeping]    │  ───▶  │   Policy #           │
//   │                           │        │   document being scanned  │        │   Date                │
//   │                           │        │   ✓ check when done       │        │   Amount              │
//   └───────────────────────────┘        └───────────────────────────┘        └───────────────────────┘
//
// One clean pass per loop (during Beat 1). From Beat 2 onward this stays in
// place as the first node of the workflow — we hide its "satellites" (incoming
// stack, exit fields) and let the card quietly loop a scan animation.

function AgentScene({ t }) {
  // The beat-1 camera centers on (~410, 410) at 2.3x zoom. In that frame we
  // want the card to be ~centered. So we lay the card around (410, 410) too.
  const AX = 410, AY = 410;

  // ---- Beat 1 timings (ms, absolute on loop clock) ----
  const docsEnter   = localE(t, [100, 900], easeOut);     // incoming docs slide in
  const docLoad     = localE(t, [700, 1300], easeInOut);  // one doc enters the agent card
  const scanSweep   = localP(t, [1300, 2000]);            // raw progress for scan line
  const fieldsPop   = localE(t, [1700, 2200], easeOut);   // extracted fields appear
  const checkPop    = localE(t, [2000, 2350], easeOut);   // green check stamps
  const docExit     = localE(t, [2250, 2600], easeInOut); // doc exits right into fields

  // Hide/dim satellites after Beat 1 ends
  const satelliteFade = t < CE.T12[0]
    ? 1
    : 1 - localE(t, CE.T12);

  // Idle scan after Beat 1 — the card keeps pulsing softly inside the workflow
  const idleScanP = t > CE.B1[1] ? ((t % 2200) / 2200) : 0;

  return (
    <g>
      {/* --- Incoming document stack (left) --- */}
      <g style={{ opacity: satelliteFade }}>
        <DocStack x={AX - 170 + (1 - docsEnter) * 40} y={AY + 8} count={3}/>
        {/* Arrow from docs → card */}
        <FlowArrow
          x1={AX - 130} y1={AY + 5}
          x2={AX - 75}  y2={AY + 5}
          opacity={docsEnter}
        />
      </g>

      {/* --- The Extraction Agent card (always present; this IS the node) --- */}
      <AgentCard
        x={AX} y={AY}
        scanP={t < CE.B1[1] ? scanSweep : idleScanP}
        docLoad={t < CE.B1[1] ? docLoad : 1}
        checkP={t < CE.B1[1] ? checkPop : 0}
        docExit={t < CE.B1[1] ? docExit : 0}
      />

      {/* --- Extracted fields (right of card) --- */}
      <g style={{ opacity: satelliteFade }}>
        {/* Arrow from card → fields */}
        <FlowArrow
          x1={AX + 75}  y1={AY + 5}
          x2={AX + 115} y2={AY + 5}
          opacity={fieldsPop}
        />
        <ExtractedFields
          x={AX + 120}
          y={AY - 28}
          p={fieldsPop}
        />
      </g>
    </g>
  );
}

// =========================================================
// AgentCard — a labeled card with the document scanning inside it.
// =========================================================
function AgentCard({ x, y, scanP, docLoad, checkP, docExit }) {
  // Card dims — compact, legible at 2.25x zoom
  const W = 140, H = 100;
  const cx = x - W / 2, cy = y - H / 2;

  return (
    <g>
      {/* Card drop shadow */}
      <rect x={cx + 3} y={cy + 4} width={W} height={H} rx="10" fill="rgba(0,0,0,0.12)"/>

      {/* Card body */}
      <rect x={cx} y={cy} width={W} height={H} rx="10"
            fill="#fff" stroke="#000" strokeWidth="2"/>

      {/* Card header strip */}
      <rect x={cx} y={cy} width={W} height="22" rx="10" fill="#dfeaff"/>
      <rect x={cx} y={cy + 12} width={W} height="10" fill="#dfeaff"/>
      <line x1={cx} y1={cy + 22} x2={cx + W} y2={cy + 22} stroke="#000" strokeWidth="2"/>

      {/* Sparkle + label */}
      <g transform={`translate(${cx + 10}, ${cy + 11})`}>
        <path d="M 0 -5 L 1.5 -1.5 L 5 0 L 1.5 1.5 L 0 5 L -1.5 1.5 L -5 0 L -1.5 -1.5 Z" fill="#397ae7"/>
      </g>
      <text x={cx + 22} y={cy + 15} fontFamily="Neue Montreal, sans-serif"
            fontSize="10" fontWeight="500" fill="#1a1d2b">Extraction Agent</text>

      {/* Status dot (right side of header) */}
      <circle cx={cx + W - 12} cy={cy + 11} r="3.5" fill="#95e17c" stroke="#000" strokeWidth="1.5"/>

      {/* Scan area — the document sits here */}
      <g clipPath={`inset(0 round 0)`}>
        {/* inner container */}
        <rect x={cx + 12} y={cy + 32} width={W - 24} height={H - 44}
              rx="4" fill="#faf7ec" stroke="#000" strokeWidth="1.5"/>

        {/* Document being processed — slides in from left during docLoad */}
        {docLoad > 0.05 && (
          <g transform={`translate(${cx + 14 + (1 - docLoad) * -40}, ${cy + 36 - docExit * 4})`}
             style={{ opacity: 1 - docExit * 0.9 }}>
            <rect x="0" y="0" width="44" height="54" rx="2" fill="#fff" stroke="#000" strokeWidth="1.5"/>
            <line x1="6" y1="8"  x2="38" y2="8"  stroke="#000" opacity=".5" strokeWidth="1"/>
            <line x1="6" y1="14" x2="32" y2="14" stroke="#000" opacity=".35" strokeWidth="1"/>
            <line x1="6" y1="20" x2="36" y2="20" stroke="#000" opacity=".35" strokeWidth="1"/>
            <line x1="6" y1="26" x2="28" y2="26" stroke="#000" opacity=".35" strokeWidth="1"/>
            <line x1="6" y1="32" x2="34" y2="32" stroke="#000" opacity=".35" strokeWidth="1"/>
            <line x1="6" y1="38" x2="24" y2="38" stroke="#000" opacity=".35" strokeWidth="1"/>
            <line x1="6" y1="44" x2="30" y2="44" stroke="#000" opacity=".35" strokeWidth="1"/>

            {/* Scan tint that fills as scanP progresses */}
            {scanP > 0 && scanP < 1 && (
              <>
                <rect x="0" y="0" width="44" height={scanP * 54} fill="#397ae7" opacity="0.14"/>
                {/* scan line */}
                <rect x="0" y={scanP * 54 - 1} width="44" height="2" fill="#397ae7"/>
              </>
            )}

            {/* Green check stamp when done */}
            {checkP > 0 && (
              <g transform={`translate(32, 44) scale(${lerp(0.4, 1, checkP)})`} style={{ opacity: checkP }}>
                <circle r="9" fill="#95e17c" stroke="#000" strokeWidth="1.5"/>
                <path d="M -4 0 L -0.5 3 L 4.5 -3" stroke="#000" strokeWidth="1.5" fill="none"
                      strokeLinecap="round" strokeLinejoin="round"/>
              </g>
            )}
          </g>
        )}
      </g>
    </g>
  );
}

// =========================================================
// DocStack — small stack of incoming docs, flat-iso lean
// =========================================================
function DocStack({ x, y, count = 3 }) {
  return (
    <g>
      {Array.from({ length: count }).map((_, i) => (
        <g key={i} transform={`translate(${x + i * 3}, ${y - i * 4})`}>
          <rect x="-15" y="-20" width="30" height="40" rx="2" fill="#fff" stroke="#000" strokeWidth="2"/>
          <line x1="-10" y1="-13" x2="10" y2="-13" stroke="#000" opacity=".45" strokeWidth="1"/>
          <line x1="-10" y1="-7"  x2="6"  y2="-7"  stroke="#000" opacity=".35" strokeWidth="1"/>
          <line x1="-10" y1="-1"  x2="8"  y2="-1"  stroke="#000" opacity=".35" strokeWidth="1"/>
          <line x1="-10" y1="5"   x2="4"  y2="5"   stroke="#000" opacity=".35" strokeWidth="1"/>
          <line x1="-10" y1="11"  x2="7"  y2="11"  stroke="#000" opacity=".35" strokeWidth="1"/>
        </g>
      ))}
    </g>
  );
}

// =========================================================
// FlowArrow — a clean → arrow between elements
// =========================================================
function FlowArrow({ x1, y1, x2, y2, opacity = 1, color = "#000" }) {
  return (
    <g style={{ opacity }}>
      <line x1={x1} y1={y1} x2={x2 - 4} y2={y2} stroke={color} strokeWidth="2"/>
      <path d={`M ${x2} ${y2} L ${x2 - 5} ${y2 - 3.5} L ${x2 - 5} ${y2 + 3.5} Z`} fill={color}/>
    </g>
  );
}

// =========================================================
// ExtractedFields — 3 simple value chips, staggered reveal
// =========================================================
function ExtractedFields({ x, y, p, opacity = 1 }) {
  if (p <= 0) return null;
  const fields = [
    { value: "Policy #",  delay: 0    },
    { value: "Date",      delay: 0.25 },
    { value: "Amount",    delay: 0.5  },
  ];
  return (
    <g style={{ opacity }}>
      {fields.map((f, i) => {
        const lp = clamp01((p - f.delay) / (1 - f.delay));
        const e = easeOut(lp);
        const dx = (1 - e) * -12;
        return (
          <g key={i} transform={`translate(${x + dx}, ${y + i * 24})`} style={{ opacity: e }}>
            <rect x="0" y="0" width="108" height="20" rx="5"
                  fill="#fff" stroke="#000" strokeWidth="1.5"/>
            <text x="54" y="13.5" fontFamily="Neue Montreal, sans-serif" fontSize="10"
                  fontWeight="400" textAnchor="middle" fill="#1a1d2b">{f.value}</text>
          </g>
        );
      })}
    </g>
  );
}

Object.assign(window, { AgentScene, AgentCard });
