// scenes-1-2.jsx — Intro + Step 1 (Photograph) + Step 2 (ChatGPT)

// ───────────────────────────────────────────────────────────
// INTRO (0–2.2s)
// Bold title reveal. Four colored cards pop in sequence behind it.
// ───────────────────────────────────────────────────────────
function SceneIntro() {
  const { progress, localTime } = useSprite();

  // Four step badges fly up from bottom, staggered
  const badges = [
    { tool: 'camera',  color: TOKENS.blue,     delay: 0.15 },
    { tool: 'chatgpt', color: TOKENS.tan,      delay: 0.30 },
    { tool: 'banana',  color: TOKENS.coral,    delay: 0.45 },
    { tool: 'kling',   color: TOKENS.lavender, delay: 0.60 },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
      {/* Background grid dots for texture */}
      <BgDots opacity={0.5}/>

      {/* Title */}
      <div style={{
        position: 'relative',
        textAlign: 'center',
        transform: `translateY(${(1 - Easing.easeOutBack(clamp(localTime / 0.6, 0, 1))) * 30}px) scale(${0.92 + 0.08 * Easing.easeOutBack(clamp(localTime / 0.6, 0, 1))})`,
        opacity: clamp(localTime / 0.35, 0, 1),
      }}>
        <div style={{
          fontFamily: TOKENS.mono,
          fontSize: 18,
          letterSpacing: '0.24em',
          color: TOKENS.inkSoft,
          marginBottom: 16,
          textTransform: 'uppercase',
        }}>
          Process &nbsp;·&nbsp; 4 Steps
        </div>
        <div style={{
          fontFamily: TOKENS.display,
          fontSize: 108,
          fontWeight: 700,
          color: TOKENS.ink,
          letterSpacing: '-0.035em',
          lineHeight: 0.95,
        }}>
          AI Hairstyle<br/>
          <span style={{ fontStyle: 'italic', color: TOKENS.coral }}>Morph</span> Videos
        </div>
        <div style={{
          fontFamily: TOKENS.display,
          fontSize: 22,
          fontWeight: 500,
          color: TOKENS.inkSoft,
          marginTop: 20,
          letterSpacing: '-0.01em',
        }}>
          How I made them — my workflow
        </div>
      </div>

      {/* Badge row fly-in */}
      <div style={{
        display: 'flex', gap: 32,
        marginTop: 60,
        alignItems: 'center',
      }}>
        {badges.map((b, i) => {
          const t = clamp((localTime - b.delay) / 0.4, 0, 1);
          const eased = Easing.easeOutBack(t);
          return (
            <div key={b.tool} style={{
              transform: `translateY(${(1 - eased) * 60}px) scale(${0.5 + 0.5 * eased})`,
              opacity: t,
            }}>
              <ToolBadge tool={b.tool} size={76}/>
            </div>
          );
        })}
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────
// STEP 1 — Photograph subject (2.2–6s, duration 3.8s)
// A "camera viewfinder" zeros in on a silhouette, flash, label "BEFORE".
// ───────────────────────────────────────────────────────────
function SceneStep1() {
  const { localTime, duration } = useSprite();

  // Timing (relative to this scene)
  const T = {
    cardIn: 0.0,     // step card + title slide in
    viewfinderIn: 0.4,
    shutter: 1.6,    // flash happens here
    polaroidOut: 2.0,// photo pops out and rotates
    beforeLabel: 2.6,
  };

  // Flash flash
  const flashT = clamp((localTime - T.shutter) / 0.4, 0, 1);
  const flashOpacity = flashT < 0.15 ? flashT / 0.15 : 1 - (flashT - 0.15) / 0.85;

  // Camera zoom/closing-in
  const vfProgress = clamp((localTime - T.viewfinderIn) / 0.9, 0, 1);
  const vfEased = Easing.easeOutCubic(vfProgress);

  // Polaroid pop
  const polT = clamp((localTime - T.polaroidOut) / 0.7, 0, 1);
  const polEased = Easing.easeOutBack(polT);

  return (
    <div style={{ position: 'absolute', inset: 0, background: TOKENS.bg }}>
      <BgDots opacity={0.3}/>

      {/* Step header top-left */}
      <StepHeader n={1} title="Photograph subject" tool="Camera" color={TOKENS.blue} startLocal={T.cardIn}/>

      {/* Main stage — viewfinder closing in on silhouette */}
      <div style={{
        position: 'absolute', left: 0, top: 80, right: 0, bottom: 0,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        {/* Silhouette (grows/settles) */}
        <div style={{
          position: 'relative',
          transform: `scale(${0.85 + 0.15 * vfEased})`,
          opacity: polT < 0.3 ? clamp(localTime / 0.4, 0, 1) : 1 - polT * 0.2,
          transition: 'none',
        }}>
          <div style={{
            background: TOKENS.blue,
            width: 360, height: 360,
            borderRadius: 24,
            border: `3px solid ${TOKENS.ink}`,
            boxShadow: `8px 8px 0 ${TOKENS.ink}`,
            display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
            overflow: 'hidden',
          }}>
            <Silhouette hairStyle="short" hairColor="#2a2320" skin="#e8c3a0" shirt="#4a6fa8" size={340}/>
          </div>

          {/* Viewfinder corners closing in */}
          {vfProgress > 0 && polT < 0.2 && (
            <ViewfinderBrackets progress={vfEased} color={TOKENS.ink}/>
          )}
        </div>

        {/* Polaroid photo flying out */}
        {polT > 0 && (
          <div style={{
            position: 'absolute',
            transform: `translate(${polEased * 20}px, ${polEased * 10}px) rotate(${-6 * polEased}deg) scale(${0.4 + 0.6 * polEased})`,
            opacity: polT,
            filter: `drop-shadow(6px 10px 0 ${TOKENS.ink})`,
          }}>
            <Polaroid label="BEFORE" accent={TOKENS.blue}>
              <div style={{
                width: 260, height: 280,
                background: TOKENS.blue,
                display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
                overflow: 'hidden',
              }}>
                <Silhouette hairStyle="short" hairColor="#2a2320" skin="#e8c3a0" shirt="#4a6fa8" size={280}/>
              </div>
            </Polaroid>
          </div>
        )}
      </div>

      {/* Camera flash overlay */}
      {flashT > 0 && flashT < 1 && (
        <div style={{
          position: 'absolute', inset: 0,
          background: '#fffceb',
          opacity: flashOpacity * 0.85,
          pointerEvents: 'none',
        }}/>
      )}
    </div>
  );
}

function ViewfinderBrackets({ progress, color }) {
  // Corner brackets that close in from the corners
  const offset = (1 - progress) * 80; // starts far out, closes to 0
  const opacity = progress;
  const bracketStyle = {
    position: 'absolute',
    width: 60, height: 60,
    borderColor: color,
    borderStyle: 'solid',
    opacity,
  };
  return (
    <>
      <div style={{ ...bracketStyle, top: -offset, left: -offset, borderWidth: '4px 0 0 4px' }}/>
      <div style={{ ...bracketStyle, top: -offset, right: -offset, borderWidth: '4px 4px 0 0' }}/>
      <div style={{ ...bracketStyle, bottom: -offset, left: -offset, borderWidth: '0 0 4px 4px' }}/>
      <div style={{ ...bracketStyle, bottom: -offset, right: -offset, borderWidth: '0 4px 4px 0' }}/>
    </>
  );
}

function Polaroid({ children, label, accent }) {
  return (
    <div style={{
      background: '#fffdf7',
      border: `3px solid ${TOKENS.ink}`,
      padding: '16px 16px 20px 16px',
      borderRadius: 4,
    }}>
      {children}
      <div style={{
        marginTop: 14,
        textAlign: 'center',
        fontFamily: TOKENS.display,
        fontWeight: 700,
        fontSize: 28,
        letterSpacing: '0.12em',
        color: TOKENS.ink,
      }}>
        {label}
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────
// STEP 2 — ChatGPT writes hairstyle prompt (6–10s, duration 4s)
// A chat window. User question typed on right; AI response types on left.
// ───────────────────────────────────────────────────────────
function SceneStep2() {
  const { localTime } = useSprite();

  const T = {
    cardIn: 0.0,
    userBubble: 0.3,
    aiBubble: 1.3,
    caretEnd: 3.6,
  };

  const userMsg = "A new hairstyle for this subject — describe it for an image generator.";
  const aiMsg = "Sleek platinum bob, chin-length, center part, glossy finish, soft studio lighting, photoreal.";

  // Reveal user message word-by-word
  const userWords = userMsg.split(' ');
  const userProg = clamp((localTime - T.userBubble) / 0.9, 0, 1);
  const userShown = Math.floor(userProg * userWords.length);

  // AI typing — chars
  const aiProg = clamp((localTime - T.aiBubble) / 1.8, 0, 1);
  const aiShown = Math.floor(aiProg * aiMsg.length);

  return (
    <div style={{ position: 'absolute', inset: 0, background: TOKENS.bg }}>
      <BgDots opacity={0.3}/>

      <StepHeader n={2} title="Write hairstyle prompt" tool="ChatGPT" color={TOKENS.tan} startLocal={T.cardIn}/>

      {/* Chat window */}
      <div style={{
        position: 'absolute',
        left: '50%', top: 180,
        transform: `translateX(-50%) translateY(${(1 - Easing.easeOutBack(clamp(localTime / 0.5, 0, 1))) * 30}px)`,
        opacity: clamp(localTime / 0.35, 0, 1),
        width: 1080,
        background: '#fff',
        border: `3px solid ${TOKENS.ink}`,
        borderRadius: 18,
        boxShadow: `8px 8px 0 ${TOKENS.ink}`,
        overflow: 'hidden',
      }}>
        {/* Window chrome */}
        <div style={{
          background: TOKENS.tan,
          padding: '14px 20px',
          borderBottom: `2px solid ${TOKENS.ink}`,
          display: 'flex', alignItems: 'center', gap: 12,
        }}>
          <div style={{ display: 'flex', gap: 6 }}>
            <Dot color="#ed6a5e"/><Dot color="#f5bd4f"/><Dot color="#62c554"/>
          </div>
          <div style={{ flex: 1, textAlign: 'center', fontFamily: TOKENS.mono, fontSize: 14, color: TOKENS.ink, letterSpacing: '0.06em' }}>
            ChatGPT — hairstyle prompt
          </div>
          <div style={{ width: 60 }}/>
        </div>

        {/* Messages */}
        <div style={{ padding: '28px 32px', minHeight: 320, display: 'flex', flexDirection: 'column', gap: 20 }}>
          {/* User bubble (right) */}
          {userProg > 0 && (
            <div style={{
              alignSelf: 'flex-end',
              maxWidth: 620,
              background: TOKENS.ink,
              color: '#faf7f2',
              padding: '16px 22px',
              borderRadius: '18px 18px 4px 18px',
              fontFamily: TOKENS.display,
              fontSize: 22,
              fontWeight: 500,
              lineHeight: 1.4,
              transform: `scale(${0.85 + 0.15 * Easing.easeOutBack(clamp(userProg * 3, 0, 1))})`,
              transformOrigin: 'bottom right',
            }}>
              {userWords.slice(0, userShown).join(' ')}
              {userShown < userWords.length && <Caret/>}
            </div>
          )}

          {/* AI bubble (left) */}
          {aiProg > 0 && (
            <div style={{
              alignSelf: 'flex-start',
              maxWidth: 720,
              background: TOKENS.tan,
              border: `2px solid ${TOKENS.ink}`,
              color: TOKENS.ink,
              padding: '16px 22px',
              borderRadius: '18px 18px 18px 4px',
              fontFamily: TOKENS.display,
              fontSize: 22,
              fontWeight: 500,
              lineHeight: 1.4,
              transform: `scale(${0.85 + 0.15 * Easing.easeOutBack(clamp(aiProg * 3, 0, 1))})`,
              transformOrigin: 'bottom left',
              display: 'flex', gap: 12, alignItems: 'flex-start',
            }}>
              <div style={{ flexShrink: 0, marginTop: 2 }}>
                <ToolMark tool="chatgpt" size={26}/>
              </div>
              <div>
                <span style={{ fontFamily: TOKENS.mono, fontSize: 13, color: TOKENS.inkSoft, display: 'block', marginBottom: 4, letterSpacing: '0.08em', textTransform: 'uppercase' }}>
                  Prompt suggestion
                </span>
                "{aiMsg.slice(0, aiShown)}
                {aiShown < aiMsg.length && <Caret/>}
                {aiShown >= aiMsg.length && '"'}
              </div>
            </div>
          )}

          {/* Copy button appears when done */}
          {aiShown >= aiMsg.length && localTime > T.aiBubble + 1.9 && (
            <div style={{
              alignSelf: 'flex-start',
              display: 'flex', alignItems: 'center', gap: 10,
              marginLeft: 38,
              background: TOKENS.ink,
              color: TOKENS.bg,
              padding: '8px 16px',
              borderRadius: 8,
              fontFamily: TOKENS.mono,
              fontSize: 14,
              letterSpacing: '0.08em',
              transform: `scale(${Easing.easeOutBack(clamp((localTime - T.aiBubble - 1.9) / 0.4, 0, 1))})`,
              boxShadow: `3px 3px 0 ${TOKENS.ink}`,
            }}>
              <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
                <rect x="3" y="3" width="7" height="8" stroke="currentColor" strokeWidth="1.3" fill="none"/>
                <rect x="5" y="1" width="7" height="8" stroke="currentColor" strokeWidth="1.3" fill="none"/>
              </svg>
              COPIED
            </div>
          )}
        </div>
      </div>
    </div>
  );
}

function Dot({ color }) {
  return <div style={{ width: 12, height: 12, borderRadius: 6, background: color, border: `1.5px solid ${TOKENS.ink}` }}/>;
}

function Caret() {
  const time = useTime();
  const on = Math.floor(time * 2.5) % 2 === 0;
  return <span style={{
    display: 'inline-block',
    width: 2, height: '0.95em',
    background: 'currentColor',
    marginLeft: 2,
    opacity: on ? 0.85 : 0,
    verticalAlign: 'text-bottom',
  }}/>;
}

// Step header (top-left): Step N · Title · tool chip
function StepHeader({ n, title, tool, color, startLocal = 0 }) {
  const { localTime } = useSprite();
  const t = clamp((localTime - startLocal) / 0.5, 0, 1);
  const eased = Easing.easeOutBack(t);
  return (
    <div style={{
      position: 'absolute',
      left: 64, top: 44,
      display: 'flex', alignItems: 'center', gap: 20,
      transform: `translateX(${(1 - eased) * -40}px)`,
      opacity: clamp(t * 1.5, 0, 1),
    }}>
      {/* Step number chip */}
      <div style={{
        width: 80, height: 80,
        background: color,
        borderRadius: 20,
        border: `3px solid ${TOKENS.ink}`,
        boxShadow: `5px 5px 0 ${TOKENS.ink}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        fontFamily: TOKENS.display,
        fontWeight: 700,
        fontSize: 44,
        color: TOKENS.ink,
        lineHeight: 1,
      }}>
        {n}
      </div>
      <div>
        <div style={{
          fontFamily: TOKENS.mono,
          fontSize: 14,
          letterSpacing: '0.18em',
          color: TOKENS.inkSoft,
          textTransform: 'uppercase',
          marginBottom: 4,
        }}>
          Step {n} · {tool}
        </div>
        <div style={{
          fontFamily: TOKENS.display,
          fontSize: 42,
          fontWeight: 700,
          color: TOKENS.ink,
          letterSpacing: '-0.02em',
          lineHeight: 1,
        }}>
          {title}
        </div>
      </div>
    </div>
  );
}

function BgDots({ opacity = 0.4 }) {
  return (
    <div style={{
      position: 'absolute', inset: 0,
      backgroundImage: `radial-gradient(${TOKENS.ink} 1.4px, transparent 1.4px)`,
      backgroundSize: '32px 32px',
      opacity,
      pointerEvents: 'none',
    }}/>
  );
}

Object.assign(window, {
  SceneIntro, SceneStep1, SceneStep2,
  Polaroid, ViewfinderBrackets, StepHeader, BgDots, Caret, Dot,
});
