// scenes/intro.jsx
// Brand opener, kinetic punchline, stats grid, and outro CTA scenes.

// ─────────────────────────────────────────────────────────────────────────
// SCENE 1 — OPENER (0–6s)
// Slashes sweep in → ALDENE wordmark assembles → "L'IA AU SERVICE DE VOTRE MÉTIER" reveals
// ─────────────────────────────────────────────────────────────────────────
function SceneOpener() {
  const { localTime, duration } = useSprite();
  const t = localTime;

  // Wordmark draw progress (slowed for a less rushed open)
  const drawP = Math.max(0, Math.min(1, (t - 0.9) / 1.8));
  // Dot pop at t=2.8s with overshoot
  const dotPop = (() => {
    const lt = Math.max(0, Math.min(1, (t - 2.8) / 0.55));
    return Easing.easeOutBack(lt);
  })();

  // Subtitle fade in at t=3.6
  const subA = Math.max(0, Math.min(1, (t - 3.6) / 0.7));
  // Tagline reveal at t=4.6
  const tagA = Math.max(0, Math.min(1, (t - 4.6) / 0.7));
  // Service icons row reveal at t=6.0
  const chipsA = Math.max(0, Math.min(1, (t - 6.0) / 0.7));

  // Exit at last 0.5s
  const exit = Math.max(0, Math.min(1, (t - (duration - 0.5)) / 0.5));
  const exitOp = 1 - exit;

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
      <PageBackground>
        {/* Top-right slash cluster */}
        <div style={{ position: 'absolute', top: -80, right: -40 }}>
          <SlashCluster appearAt={0} stagger={0.1} duration={0.7} origin="tr"/>
        </div>
        {/* Bottom-left slash cluster */}
        <div style={{ position: 'absolute', bottom: -120, left: -80 }}>
          <SlashCluster appearAt={0.3} stagger={0.1} duration={0.7} origin="bl" scale={0.7} rotate={-24}/>
        </div>

        {/* Centered wordmark + tagline */}
        <div style={{
          position: 'absolute',
          left: '50%', top: '50%',
          transform: 'translate(-50%, -55%)',
          display: 'flex', flexDirection: 'column',
          alignItems: 'center', gap: 36,
        }}>
          {/* Eyebrow */}
          <div style={{
            opacity: subA,
            transform: `translateY(${(1 - subA) * 12}px)`,
          }}>
            <Eyebrow size={16}>V O S &nbsp; S U C C È S</Eyebrow>
          </div>

          {/* Wordmark */}
          <AldeneWordmark height={150} drawProgress={drawP} dotPop={dotPop} />

          {/* IA badge — appears with wordmark */}
          <div style={{
            opacity: subA,
            transform: `translateY(${(1 - subA) * 12}px)`,
            display: 'flex', alignItems: 'center', gap: 16,
          }}>
            <div style={{
              padding: '8px 20px',
              background: ALD.svc.ia,
              borderRadius: 999,
              fontFamily: FONT_DISPLAY,
              fontWeight: 700,
              fontSize: 22,
              letterSpacing: '.18em',
              color: ALD.navy,
            }}>I A &nbsp;·&nbsp; A U T O M A T I S A T I O N</div>
          </div>

          {/* Tagline */}
          <div style={{
            marginTop: 12,
            opacity: tagA,
            transform: `translateY(${(1 - tagA) * 16}px)`,
            textAlign: 'center',
            maxWidth: 1100,
          }}>
            <div style={{
              fontFamily: FONT_DISPLAY,
              fontWeight: 800,
              fontSize: 62,
              lineHeight: 1.05,
              letterSpacing: '-0.01em',
              textTransform: 'uppercase',
              color: ALD.white,
            }}>
              L'IA au service<br/>de votre métier<span style={{ color: ALD.yellow }}>.</span>
            </div>
          </div>

          {/* Service chips row */}
          <div style={{
            marginTop: 8,
            opacity: chipsA,
            transform: `translateY(${(1 - chipsA) * 12}px)`,
            display: 'flex', gap: 32,
          }}>
            <ServiceChip color={ALD.svc.ia}    label="IA générative"   size={18}/>
            <ServiceChip color={ALD.svc.info}  label="Automatisation"  size={18}/>
            <ServiceChip color={ALD.svc.cyber} label="Sur-mesure"      size={18}/>
          </div>
        </div>
      </PageBackground>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// SCENE 2 — HOOK (5.5–11.5s, ~6s)
// Short kinetic hook positioning Aldene IA before the app showcase.
// ─────────────────────────────────────────────────────────────────────────
function ScenePromise() {
  const { localTime, duration } = useSprite();
  const t = localTime;

  // Three short statements, then landing
  // 0.0–1.0  : "Pas de l'IA pour faire de l'IA."
  // 1.0–2.0  : "De l'IA qui tourne."
  // 2.0–3.0  : "Tous les jours. Pour de vrai."
  // 2.5–3.0  : flash
  // 3.0–6.0  : landing "23 applications. Un aperçu."

  const statements = [
    { txt: 'Pas de l\u2019IA',  emph: 'pour faire de l\u2019IA', tail: '.' },
    { txt: 'De l\u2019IA',       emph: 'qui tourne',             tail: '.' },
    { txt: 'Tous les jours.',  emph: 'Pour de vrai',           tail: '.' },
  ];

  const slot = 1.7;
  const idx = Math.floor(t / slot);
  const activePhase = idx < 3;
  const localT = t - idx * slot;

  // Yellow flash on transition to landing
  const flashStart = 4.85;
  const flashEnd = 5.35;
  const flashAlpha = t >= flashStart && t < flashEnd
    ? (1 - Math.abs((t - (flashStart + flashEnd) / 2) / ((flashEnd - flashStart) / 2)))
    : 0;

  // Final tagline appears at t=5.1
  const finalA = Math.max(0, Math.min(1, (t - 5.1) / 0.9));

  // exit fade
  const exit = Math.max(0, Math.min(1, (t - (duration - 0.5)) / 0.5));
  const exitOp = 1 - exit;

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
      <PageBackground>
        <div style={{ position: 'absolute', top: -60, right: -30, opacity: 0.5 }}>
          <SlashCluster appearAt={0} stagger={0.06} duration={0.4} scale={0.5} origin="tr"/>
        </div>

        {/* Statement stack */}
        {activePhase && (
          <div style={{
            position: 'absolute',
            left: 120, top: '50%',
            transform: 'translateY(-50%)',
            width: 1700,
          }}>
            <PromiseLine
              key={idx}
              prefix={statements[idx].txt}
              emph={statements[idx].emph}
              tail={statements[idx].tail}
              localT={localT}
              slot={slot}
            />
            <div style={{ marginTop: 60, display: 'flex', gap: 14 }}>
              {statements.map((_, i) => (
                <div key={i} style={{
                  width: i === idx ? 48 : 14,
                  height: 6, borderRadius: 4,
                  background: i <= idx ? ALD.yellow : 'rgba(255,255,255,0.18)',
                  transition: 'width 180ms cubic-bezier(.2,.7,.2,1)',
                }}/>
              ))}
            </div>
          </div>
        )}

        {/* Yellow flash */}
        <div style={{
          position: 'absolute', inset: 0,
          background: ALD.yellow,
          opacity: flashAlpha * 0.85,
          pointerEvents: 'none',
        }}/>

        {/* Final landing */}
        {finalA > 0 && (
          <div style={{
            position: 'absolute',
            left: 120, top: '50%',
            transform: `translate(0, calc(-50% + ${(1 - finalA) * 24}px))`,
            opacity: finalA,
            maxWidth: 1700,
          }}>
            <Eyebrow size={18} style={{ marginBottom: 30 }}>A L D E N E &nbsp; I A</Eyebrow>
            <div style={{
              fontFamily: FONT_DISPLAY, fontWeight: 800,
              fontSize: 132, lineHeight: 0.95,
              letterSpacing: '-0.015em', textTransform: 'uppercase',
              color: ALD.white,
            }}>
              <span style={{ color: ALD.yellow }}>23 applications.</span><br/>
              Un aperçu<span style={{ color: ALD.yellow }}>.</span>
            </div>
            <div style={{
              marginTop: 40, fontFamily: FONT_BODY, fontWeight: 400, fontSize: 26,
              color: '#C7CADD', maxWidth: 900, lineHeight: 1.4,
            }}>
              Ce que nos équipes utilisent au quotidien,
              <br/>et ce qu'on peut bâtir pour vous.
            </div>
          </div>
        )}
      </PageBackground>
    </div>
  );
}

// Helper: a single PromiseLine that wipes in
function PromiseLine({ prefix, emph, tail, localT, slot }) {
  const inA = Math.max(0, Math.min(1, localT / 0.45));
  const outStart = slot - 0.4;
  const outA = localT > outStart ? Math.max(0, Math.min(1, (localT - outStart) / 0.4)) : 0;
  const ty = (1 - Easing.easeOutCubic(inA)) * 40 + outA * -20;
  const op = Easing.easeOutCubic(inA) * (1 - outA);

  return (
    <div style={{
      transform: `translateY(${ty}px)`,
      opacity: op,
      willChange: 'transform, opacity',
    }}>
      <Eyebrow size={18} style={{ marginBottom: 30 }}>
        N O T R E &nbsp; A P P R O C H E
      </Eyebrow>
      <div style={{
        fontFamily: FONT_DISPLAY,
        fontWeight: 800,
        fontSize: 124,
        lineHeight: 0.95,
        letterSpacing: '-0.015em',
        textTransform: 'uppercase',
        color: ALD.white,
      }}>
        {prefix}<br/>
        <span style={{ color: ALD.yellow }}>{emph}</span>{tail}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// SCENE 6 — STATS GRID (44–52s)
// Four big stat cards reveal with counters
// ─────────────────────────────────────────────────────────────────────────
function SceneStats() {
  const { localTime, duration } = useSprite();
  const t = localTime;

  const stats = [
    { value: '23',      label: 'applications IA',  sub: 'développées et déployées en interne',  color: ALD.svc.ia },
    { value: '200+',    label: 'workflows n8n',    sub: 'actifs en production · 65 423 exéc.', color: ALD.svc.cyber },
    { value: '27h',     label: 'économisées /sem', sub: 'rendues à nos équipes chaque semaine', color: ALD.yellow },
  ];

  const headA = Math.max(0, Math.min(1, (t - 0.1) / 0.6));
  const exit = Math.max(0, Math.min(1, (t - (duration - 0.5)) / 0.5));
  const exitOp = 1 - exit;

  return (
    <div style={{ position: 'absolute', inset: 0, opacity: exitOp }}>
      <PageBackground>
        <div style={{ position: 'absolute', bottom: -100, left: -60, opacity: 0.55 }}>
          <SlashCluster appearAt={0} stagger={0.08} duration={0.55} scale={0.55} origin="bl"/>
        </div>

        {/* Header */}
        <div style={{
          position: 'absolute', left: 110, top: 110,
          opacity: headA,
          transform: `translateY(${(1 - headA) * 16}px)`,
        }}>
          <Eyebrow size={18} style={{ marginBottom: 22 }}>A L D E N E &nbsp; · &nbsp; E N &nbsp; C H I F F R E S</Eyebrow>
          <div style={{
            fontFamily: FONT_DISPLAY, fontWeight: 800, fontSize: 88,
            lineHeight: 0.95, letterSpacing: '-0.01em',
            textTransform: 'uppercase', color: ALD.white,
            maxWidth: 1200,
          }}>
            Du concret<span style={{ color: ALD.yellow }}>.</span> Pas du buzz.
          </div>
        </div>

        {/* Grid */}
        <div style={{
          position: 'absolute',
          left: 110, right: 110, bottom: 110,
          display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)',
          gap: 28,
        }}>
          {stats.map((s, i) => {
            const inDelay = 0.8 + i * 0.18;
            const inA = Math.max(0, Math.min(1, (t - inDelay) / 0.55));
            const e = Easing.easeOutBack(inA);
            return (
              <div key={i} style={{
                background: ALD.navy700,
                border: '1px solid rgba(255,255,255,0.10)',
                borderRadius: 16,
                padding: '36px 32px',
                opacity: inA,
                transform: `translateY(${(1 - e) * 60}px)`,
                boxShadow: '0 12px 36px rgba(0,0,0,0.25)',
                position: 'relative',
                overflow: 'hidden',
                minHeight: 280,
                display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
              }}>
                <div style={{
                  width: 36, height: 36, borderRadius: 999,
                  background: s.color,
                  marginBottom: 24,
                }}/>
                <div style={{
                  fontFamily: FONT_DISPLAY,
                  fontWeight: 800,
                  fontSize: 84,
                  lineHeight: 1,
                  color: ALD.white,
                  letterSpacing: '-0.02em',
                  marginBottom: 12,
                }}>
                  {s.value}
                </div>
                <div>
                  <div style={{
                    fontFamily: FONT_DISPLAY,
                    fontWeight: 700,
                    fontSize: 22,
                    color: ALD.white,
                    textTransform: 'uppercase',
                    letterSpacing: '.04em',
                    marginBottom: 8,
                  }}>{s.label}</div>
                  <div style={{
                    fontFamily: FONT_BODY,
                    fontSize: 15,
                    color: '#C7CADD',
                    lineHeight: 1.4,
                  }}>{s.sub}</div>
                </div>
              </div>
            );
          })}
        </div>
      </PageBackground>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// SCENE 7 — OUTRO (52–60s)
// Logo center, 3 pillars, contact, CTA
// ─────────────────────────────────────────────────────────────────────────
function SceneOutro({ contact }) {
  const { localTime, duration } = useSprite();
  const t = localTime;

  const drawP = Math.max(0, Math.min(1, (t - 0.2) / 1.2));
  const dotPop = (() => {
    const lt = Math.max(0, Math.min(1, (t - 1.3) / 0.5));
    return Easing.easeOutBack(lt);
  })();
  const tagA  = Math.max(0, Math.min(1, (t - 1.7) / 0.6));
  const pillarsA = Math.max(0, Math.min(1, (t - 2.4) / 0.7));
  const ctaA  = Math.max(0, Math.min(1, (t - 3.2) / 0.6));
  const contactA = Math.max(0, Math.min(1, (t - 3.8) / 0.6));
  const qrA = Math.max(0, Math.min(1, (t - 3.4) / 0.7));

  return (
    <div style={{ position: 'absolute', inset: 0 }}>
      <PageBackground>
        <div style={{ position: 'absolute', top: -80, right: -40, opacity: 0.7 }}>
          <SlashCluster appearAt={0} stagger={0.1} duration={0.7} origin="tr"/>
        </div>
        <div style={{ position: 'absolute', bottom: -110, left: -60, opacity: 0.6 }}>
          <SlashCluster appearAt={0.2} stagger={0.1} duration={0.7} origin="bl" scale={0.7}/>
        </div>

        <div style={{
          position: 'absolute',
          left: 0, right: 0, top: 0, bottom: 0,
          display: 'flex', flexDirection: 'column',
          alignItems: 'center', justifyContent: 'center',
          gap: 0,
        }}>
          {/* Logo */}
          <AldeneWordmark height={120} drawProgress={drawP} dotPop={dotPop}/>

          {/* tagline */}
          <div style={{
            marginTop: 48,
            opacity: tagA,
            transform: `translateY(${(1 - tagA) * 16}px)`,
            textAlign: 'center',
          }}>
            <div style={{
              fontFamily: FONT_DISPLAY, fontWeight: 700, fontSize: 24,
              letterSpacing: '.18em', color: ALD.white,
              textTransform: 'uppercase',
            }}>
              Vos succès <span style={{ color: ALD.yellow }}>·</span> Nos hommes, nos solutions
            </div>
          </div>

          {/* pillars */}
          <div style={{
            marginTop: 36,
            opacity: pillarsA,
            transform: `translateY(${(1 - pillarsA) * 16}px)`,
            display: 'flex', gap: 70,
            fontFamily: FONT_DISPLAY, fontWeight: 700, fontSize: 17,
            letterSpacing: '.32em',
            color: 'rgba(255,255,255,.6)',
            textTransform: 'uppercase',
          }}>
            <span>F I A B I L I T É</span>
            <span style={{ color: ALD.yellow }}>·</span>
            <span>D Y N A M I S M E</span>
            <span style={{ color: ALD.yellow }}>·</span>
            <span>H U M A I N</span>
          </div>

          {/* CTA + QR row */}
          <div style={{
            marginTop: 60,
            display: 'flex', alignItems: 'center', gap: 64,
          }}>
            {/* Left: CTA pill */}
            <div style={{
              opacity: ctaA,
              transform: `scale(${0.92 + 0.08 * ctaA})`,
              padding: '22px 50px',
              background: ALD.yellow,
              borderRadius: 999,
              color: ALD.navy,
              fontFamily: FONT_DISPLAY, fontWeight: 800,
              fontSize: 30, letterSpacing: '.04em',
              textTransform: 'uppercase',
            }}>
              Discutons de votre projet IA<span>.</span>
            </div>

            {/* Right: QR panel */}
            <div style={{
              opacity: qrA,
              transform: `translateY(${(1 - qrA) * 18}px)`,
              display: 'flex', alignItems: 'center', gap: 22,
              padding: '16px 24px 16px 16px',
              background: ALD.white,
              borderRadius: 16,
              boxShadow: '0 16px 48px rgba(0,0,0,.4)',
            }}>
              <img
                src="https://api.qrserver.com/v1/create-qr-code/?size=240x240&data=https%3A%2F%2Faldene.ai&color=1E2442&bgcolor=FFFFFF&qzone=1"
                alt="QR code aldene.ai"
                style={{
                  width: 132, height: 132, display: 'block',
                  borderRadius: 6,
                }}
              />
              <div style={{
                display: 'flex', flexDirection: 'column', gap: 6,
                color: ALD.navy, fontFamily: FONT_BODY,
              }}>
                <div style={{
                  fontFamily: FONT_DISPLAY, fontWeight: 700, fontSize: 11,
                  letterSpacing: '.20em', textTransform: 'uppercase',
                  color: '#8A90B0',
                }}>S C A N N E Z &nbsp; →</div>
                <div style={{
                  fontFamily: FONT_DISPLAY, fontWeight: 800, fontSize: 24,
                  letterSpacing: '-0.01em',
                }}>aldene<span style={{ color: ALD.yellow }}>.</span>ai</div>
                <div style={{
                  fontFamily: FONT_BODY, fontSize: 12,
                  color: '#4A5175', lineHeight: 1.4,
                  maxWidth: 200,
                }}>Diagnostic IA gratuit en 4 minutes</div>
              </div>
            </div>
          </div>

          {/* Contact */}
          <div style={{
            marginTop: 44,
            opacity: contactA,
            transform: `translateY(${(1 - contactA) * 10}px)`,
            display: 'flex', gap: 48,
            fontFamily: FONT_BODY, fontSize: 20,
            color: '#C7CADD',
            alignItems: 'center',
          }}>
            <span><b style={{ color: ALD.white, fontWeight: 600 }}>{contact.phone}</b></span>
            <span style={{ color: ALD.yellow }}>·</span>
            <span><b style={{ color: ALD.white, fontWeight: 600 }}>{contact.web}</b></span>
            <span style={{ color: ALD.yellow }}>·</span>
            <span>{contact.cities}</span>
          </div>
        </div>
      </PageBackground>
    </div>
  );
}

Object.assign(window, { SceneOpener, ScenePromise, SceneStats, SceneOutro });
