// scene-act3.jsx — Act III: Phase 2 BRD with full eval loop
// Total ~22s. Beats:
//  0.0–2.0   split: cream notebook (left, summarized) + black terminal panel (right, empty)
//  2.0–6.0   prompt types into terminal (green Courier, scanlines)
//  6.0–10.0  terminal fills with scrolling FR-### requirements; orange counter ticks 1→47→89
// 10.0–11.0  terminal shrinks to a card; second card slides in: NEW CHAT — EVAL PROTOCOL
// 11.0–14.0  cycle arrow loops; findings card spawns with 3 bullets (✗ ✗ ✓)
// 14.0–16.5  loop back to BRD card; pulses orange
// 16.5–19.5  ✓ FINAL types in: "BRD v1.4 — FINAL"
// 19.5–22.0  hold

function SceneAct3() {
  return (
    <Timeline duration={26800} sceneId="act3" label="Act III — Phase 2: BRD + Evaluation Loop">
      <CaptionScene>
        {/* split bg: left cream, right terminal */}
        <div className="scene-bg" style={{ position: 'absolute', inset: 0 }}>
          <div className="scene-cream-bg" style={{ right: '50%' }} />
          <div className="scene-terminal-bg" style={{ left: '50%' }} />
        </div>

        <PhaseIntroCard
          from={0}
          num="02"
          name="BRD"
          who="😍 Human + 🤖 Claude"
          tone="cream"
          summary="Turn the charter into testable requirements — and let Claude grade its own work."
        />

        <TimeShift offset={4800}>
          <Act3Split />
          <Act3EvalLoop />
          <Act3Final />

          <SceneCaption text="You prompt. Claude drafts." from={3000} typeDur={1200} hold={1800} terminal />
          <SceneCaption text="89 requirements in 90 seconds." from={7400} typeDur={1300} hold={2000} terminal />
          <SceneCaption text="Evaluation Loop — Claude grades its own work." from={11400} typeDur={1700} hold={2000} terminal />
          <SceneCaption text="Lock it down." from={17500} typeDur={900} hold={2500} terminal />
        </TimeShift>
      </CaptionScene>
    </Timeline>
  );
}

// ── 0–10s — Split scene: notebook left, terminal right ──
function Act3Split() {
  const t = useT();
  // After 10s, the terminal shrinks to a card → fade this out
  const op = t < 10000 ? 1 : Math.max(0, 1 - (t - 10000) / 600);
  if (op === 0) return null;

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: op }}>
      {/* LEFT — Project Charter (full content) */}
      <div style={{
        position: 'absolute',
        left: 60, top: 100, width: 880, height: 900,
        background: 'var(--cream-paper)',
        border: '1px solid var(--cream-rule)',
        borderRadius: 8,
        padding: '36px 44px',
        boxShadow: '0 24px 60px -28px rgba(80,60,30,0.25)',
      }}>
        <div style={{
          display: 'flex', justifyContent: 'space-between', alignItems: 'baseline',
          marginBottom: 6,
        }}>
          <div style={{
            fontFamily: 'var(--font-mono)', fontSize: 14, letterSpacing: '0.18em',
            color: 'var(--ink-faint)', textTransform: 'uppercase',
          }}>Project Charter</div>
          <div style={{
            fontFamily: 'var(--font-mono)', fontSize: 13, letterSpacing: '0.14em',
            color: 'var(--ink-faint)',
          }}>😍 HUMAN ONLY</div>
        </div>
        <div style={{
          height: 1, background: 'var(--cream-rule)', marginBottom: 22,
        }} />

        <div style={{ fontFamily: 'var(--font-serif)', color: 'var(--ink)' }}>
          <div style={{
            fontFamily: 'var(--font-mono)', color: 'var(--orange)',
            fontSize: 14, letterSpacing: '0.16em', marginBottom: 6,
          }}>PROBLEM STATEMENT</div>
          <p style={{ margin: '0 0 22px', fontSize: 19, lineHeight: 1.5 }}>
            Brokers run on a 60-day escrow clock. Every day of uncertainty is a day closer to a failed deal.
          </p>

          <div style={{
            fontFamily: 'var(--font-mono)', color: 'var(--orange)',
            fontSize: 14, letterSpacing: '0.16em', marginBottom: 6,
          }}>GOAL STATEMENT</div>
          <p style={{ margin: '0 0 22px', fontSize: 19, lineHeight: 1.5 }}>
            Speed to Terms — deliver a credible term summary to the borrower as fast as possible.
          </p>

          <div style={{
            fontFamily: 'var(--font-mono)', color: 'var(--orange)',
            fontSize: 14, letterSpacing: '0.16em', marginBottom: 8,
          }}>VOICE OF CUSTOMER</div>
          <div style={{
            fontFamily: 'var(--font-serif)', fontStyle: 'italic',
            fontSize: 18, lineHeight: 1.65, color: 'var(--ink-soft)',
            paddingLeft: 16, borderLeft: '2px solid var(--orange-tint)',
          }}>
            <div style={{ marginBottom: 6 }}>"I don't want to be asked for the same thing twice."</div>
            <div style={{ marginBottom: 6 }}>"Why did you go dark on me?"</div>
            <div>"Who has the ball right now?"</div>
          </div>
        </div>
      </div>

      {/* RIGHT — terminal */}
      <div style={{
        position: 'absolute',
        left: 1000, top: 120, width: 840, height: 850,
        background: '#0A0A0A',
        border: '1px solid rgba(0,255,0,0.2)',
        borderRadius: 8,
        overflow: 'hidden',
        boxShadow: '0 30px 80px -20px rgba(0,0,0,0.5)',
      }}>
        <div style={{
          height: 36, background: '#0F0F0F',
          borderBottom: '1px solid rgba(0,255,0,0.12)',
          display: 'flex', alignItems: 'center', padding: '0 14px',
          gap: 8, fontFamily: 'var(--font-mono)', fontSize: 13,
          color: 'rgba(0,255,0,0.4)', letterSpacing: '0.08em',
        }}>
          <span style={{ width: 12, height: 12, borderRadius: '50%', background: 'rgba(0,255,0,0.3)' }} />
          <span style={{ width: 12, height: 12, borderRadius: '50%', background: 'rgba(255,176,0,0.45)' }} />
          <span style={{ width: 12, height: 12, borderRadius: '50%', background: 'rgba(184,48,48,0.55)' }} />
          <span style={{ marginLeft: 14, textTransform: 'uppercase' }}>claude · BRD draft</span>
        </div>
        <div style={{
          padding: '32px 38px',
          fontFamily: 'var(--font-mono)', color: '#00FF00',
          fontSize: 22, lineHeight: 1.55,
          position: 'relative',
        }}>
          {/* prompt — types 2.0–5.5 */}
          <div>
            <span style={{ color: '#00B800' }}>&gt; </span>
            <Typewriter text="You are a senior BA. Convert this charter" from={2000} duration={700} caret={false} />
          </div>
          <div style={{ paddingLeft: 22 }}>
            <Typewriter text="into a numbered BRD with testable" from={2700} duration={650} caret={false} />
          </div>
          <div style={{ paddingLeft: 22 }}>
            <Typewriter text='requirements. "The system shall..."' from={3350} duration={750} caret={t < 5000} blinkAfter />
          </div>

          {/* Generated requirements — appear 6.0+ */}
          <Act3GeneratedReqs />

          {/* counter */}
          <Act3Counter />
        </div>
      </div>
    </div>
  );
}

function Act3GeneratedReqs() {
  const t = useT();
  const reqs = [
    'FR-001  The system shall capture property...',
    'FR-001a The property address shall be...',
    'FR-014  The system shall notify in a timely...',
    'FR-022  The deal shall transition states...',
    'FR-035  The system shall validate matrix...',
    'FR-048  The CIO portal shall display the...',
    'FR-062  The AI Underwriter shall calculate...',
    'FR-074  The audit log shall capture every...',
    'FR-089  The system shall finalize the TAP...',
  ];
  if (t < 5800) return null;
  return (
    <div style={{ marginTop: 22, color: '#00FF00', fontSize: 18, lineHeight: 1.6 }}>
      {reqs.map((r, i) => {
        const start = 5800 + i * 380;
        if (t < start) return null;
        const op = Math.min(1, (t - start) / 250);
        return (
          <div key={i} style={{ opacity: op }}>
            <Typewriter text={r} from={start} duration={350} caret={false} />
          </div>
        );
      })}
    </div>
  );
}

function Act3Counter() {
  const t = useT();
  if (t < 6000) return null;
  // 6.0 → 9.5 ramps 1 → 89
  const p = Math.min(1, Math.max(0, (t - 6000) / 3500));
  const eased = 1 - Math.pow(1 - p, 1.6);
  const n = Math.floor(1 + eased * 88);
  return (
    <div style={{
      position: 'absolute',
      top: 16, right: 22,
      fontFamily: 'var(--font-mono)',
      fontSize: 18, color: '#FFB000',
      border: '1px solid rgba(255,176,0,0.35)',
      padding: '6px 12px', borderRadius: 4,
      background: 'rgba(255,176,0,0.08)',
      letterSpacing: '0.1em',
    }}>
      requirements: {n}/89
    </div>
  );
}

// ── 10–17s — Eval loop diagram ──────────────────────────
function Act3EvalLoop() {
  const t = useT();
  if (t < 9700 || t > 18800) return null;
  const op = t < 10500 ? Math.min(1, (t - 9700) / 800) : (t > 18000 ? Math.max(0, 1 - (t - 18000) / 800) : 1);

  // Three cards in a triangle around a center
  const cards = [
    { label: 'BRD v1.0', sub: 'draft', x: 760,  y: 270, appear: 10000, color: 'cream' },
    { label: 'NEW CHAT',  sub: 'eval protocol', x: 1340, y: 540, appear: 11000, color: 'cream' },
    { label: 'FINDINGS',  sub: '', x: 760, y: 810, appear: 13000, color: 'cream' },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: op }}>
      {/* connecting cycle arrows */}
      <svg width="1920" height="1080" style={{ position: 'absolute', inset: 0 }}>
        <defs>
          <marker id="loop-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse">
            <path d="M 0 0 L 10 5 L 0 10 z" fill="var(--orange)" />
          </marker>
        </defs>
        {/* BRD → NEW CHAT */}
        <DrawPath d="M 1000 320 C 1180 320, 1260 420, 1340 520" from={11200} duration={500} stroke="var(--orange)" strokeWidth="3" />
        {/* NEW CHAT → FINDINGS */}
        <DrawPath d="M 1340 720 C 1260 800, 1100 820, 1000 820" from={13200} duration={500} stroke="var(--orange)" strokeWidth="3" />
        {/* FINDINGS → BRD (loop back) */}
        <DrawPath d="M 760 770 C 600 700, 600 400, 760 330" from={15400} duration={700} stroke="var(--orange)" strokeWidth="3" />

        {/* arrow heads (separate so they stamp at end) */}
        {t > 11700 && <polygon points="1340,520 1330,510 1335,532" fill="var(--orange)" />}
        {t > 13700 && <polygon points="1000,820 1010,810 990,800" fill="var(--orange)" />}
        {t > 16100 && <polygon points="760,330 768,320 778,340" fill="var(--orange)" />}

        {/* center cycle badge */}
        <g transform="translate(1050, 540)" opacity={t > 11000 ? Math.min(1, (t - 11000)/600) : 0}>
          <circle r="100" fill="var(--cream-base)" stroke="var(--orange)" strokeWidth="3" />
          <text textAnchor="middle" y="-22" fontFamily="var(--font-mono)" fontSize="40" fill="var(--orange)" fontWeight="700">↻</text>
          <text textAnchor="middle" y="14" fontFamily="var(--font-mono)" fontSize="14" fill="var(--orange)" letterSpacing="2" fontWeight="700">EVALUATION</text>
          <text textAnchor="middle" y="34" fontFamily="var(--font-mono)" fontSize="14" fill="var(--orange)" letterSpacing="2" fontWeight="700">LOOP</text>
        </g>
      </svg>

      {cards.map((c, i) => {
        if (t < c.appear) return null;
        const cardOp = Math.min(1, (t - c.appear) / 400);
        const ty = (1 - cardOp) * 16;
        return (
          <div key={i} style={{
            position: 'absolute',
            left: c.x - 160, top: c.y - 100,
            width: 320, height: 200,
            background: 'var(--cream-base)',
            border: '2px solid var(--orange)',
            borderRadius: 6,
            boxShadow: '0 18px 40px -16px rgba(80,60,30,0.25)',
            padding: '20px 24px',
            opacity: cardOp,
            transform: `translateY(${ty}px)`,
          }}>
            <div style={{
              fontFamily: 'var(--font-mono)',
              fontSize: 14, color: 'var(--ink-faint)',
              letterSpacing: '0.18em', textTransform: 'uppercase',
              marginBottom: 8,
            }}>{c.sub}</div>
            <div style={{
              fontFamily: 'var(--font-mono)',
              fontSize: 28, color: 'var(--orange)',
              fontWeight: 700, letterSpacing: '0.04em',
            }}>{c.label}</div>

            {/* findings card content */}
            {i === 2 && t > 13400 && (
              <div style={{ marginTop: 12, fontFamily: 'var(--font-mono)', fontSize: 16, lineHeight: 1.6, color: 'var(--ink)' }}>
                <Act3Bullet from={13700} sym="✗" symColor="#B83030" text="FR-014: ambiguous" />
                <Act3Bullet from={14100} sym="✗" symColor="#B83030" text="FR-022: state gap" />
                <Act3Bullet from={14500} sym="✓" symColor="var(--success-green)" text="87/89 pass" />
              </div>
            )}
          </div>
        );
      })}
    </div>
  );
}

function Act3Bullet({ from, sym, symColor, text }) {
  const t = useT();
  if (t < from) return null;
  const op = Math.min(1, (t - from) / 300);
  return (
    <div style={{ opacity: op, display: 'flex', gap: 10 }}>
      <span style={{ color: symColor, fontWeight: 700 }}>{sym}</span>
      <span><Typewriter text={text} from={from} duration={400} caret={false} /></span>
    </div>
  );
}

// ── 17–22s — ✓ FINAL ─────────────────────────────────────
function Act3Final() {
  const t = useT();
  if (t < 18500) return null;
  const op = Math.min(1, (t - 18500) / 600);
  const ty = (1 - op) * 24;
  return (
    <div style={{
      position: 'absolute',
      left: 0, right: 0, top: 440,
      textAlign: 'center',
      opacity: op,
      transform: `translateY(${ty}px)`,
      zIndex: 4,
    }}>
      <div style={{
        display: 'inline-flex',
        alignItems: 'center', gap: 24,
        background: 'rgba(254,252,248,0.96)',
        padding: '32px 56px',
        borderRadius: 8,
        border: '2px solid var(--orange)',
        boxShadow: '0 20px 60px -16px rgba(0,0,0,0.4)',
      }}>
        <span style={{
          width: 78, height: 78,
          border: '5px solid var(--orange)',
          borderRadius: 14,
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          color: 'var(--orange)', fontSize: 56, fontWeight: 700,
          fontFamily: 'var(--font-mono)',
        }}>✓</span>
        <span style={{
          fontFamily: 'var(--font-mono)',
          fontSize: 56, color: 'var(--orange)',
          fontWeight: 700, letterSpacing: '0.08em',
        }}>
          <Typewriter text="BRD v1.4 — FINAL" from={18900} duration={1100} caret={false} />
        </span>
      </div>
    </div>
  );
}

window.SceneAct3 = SceneAct3;
