// scenes-intro.jsx — White Cube edition
// Cold open with official AK logo, intros, transitions, closing.

// ── Cold open: AK logo ──────────────────────────────────────────────────────
function ColdOpenScene({ start, end }) {
  return (
    <Sprite start={start} end={end}>
      <SceneShell>
        <ColdOpenInner/>
      </SceneShell>
    </Sprite>
  );
}

function ColdOpenInner() {
  const { localTime, duration } = useSprite();

  const logoT = clamp((localTime - 0.3) / 0.9, 0, 1);
  const logoE = Easing.easeOutCubic(logoT);

  const captionT = clamp((localTime - 1.0) / 0.7, 0, 1);
  const captionE = Easing.easeOutCubic(captionT);

  const ruleT = clamp((localTime - 0.8) / 0.5, 0, 1);

  const globeT = clamp((localTime - 0.6) / 1.0, 0, 1);
  const globeE = Easing.easeOutCubic(globeT);

  const float = Math.sin(localTime * 0.5) * 2;

  return (
    <>
      {/* Soft editorial background */}
      <BrandSoftBG opacity={clamp(localTime / 0.6, 0, 1)} intensity={0.10}/>

      {/* Soft white scrim for legibility behind logo+caption */}
      <div style={{
        position: 'absolute',
        left: 0, top: 0, bottom: 0, width: '62%',
        background: `linear-gradient(90deg, ${AK.paper} 0%, ${AK.paper}F2 55%, ${AK.paper}00 100%)`,
        pointerEvents: 'none',
        opacity: clamp(localTime / 0.5, 0, 1),
      }}/>

      {/* ─── LEFT: logo + caption + rule ─────────────────────────────── */}
      <div style={{
        position: 'absolute',
        left: 140, top: 300,
        width: 860,
        opacity: logoE,
        transform: `translateY(${(1-logoE)*8 + float}px)`,
        filter: 'drop-shadow(0 2px 18px rgba(10,10,10,0.08))',
      }}>
        <img src="assets/logos/ak-networks.png" alt="AK Networks"
          style={{ width: 760, height: 'auto', display: 'block' }}
        />
      </div>

      {/* Red rule */}
      <div style={{
        position: 'absolute', left: 140, top: 560,
        width: 140, height: 5,
        background: AK.red,
        transform: `scaleX(${ruleT})`,
        transformOrigin: 'left',
        boxShadow: `0 0 20px ${AK.red}55`,
      }}/>

      {/* Caption */}
      <div style={{
        position: 'absolute',
        left: 140, top: 620,
        width: 880,
        opacity: captionE,
        transform: `translateY(${(1-captionE)*10}px)`,
      }}>
        <div style={{
          fontFamily: AK.display, fontWeight: 900,
          fontSize: 56, color: AK.ink,
          letterSpacing: '-0.028em', lineHeight: 1.08,
          textWrap: 'balance',
          textShadow: '0 1px 0 rgba(255,255,255,0.6)',
        }}>
          Identidade · Zero Trust<br/>
          IA Segura · <span style={{ color: AK.red }}>Resiliência</span>
        </div>
      </div>

      {/* ─── RIGHT: interactive globe ──────────────────────────────── */}
      <div style={{
        position: 'absolute',
        right: 80, top: '50%',
        transform: `translateY(-50%) scale(${0.92 + 0.08 * globeE})`,
        opacity: globeE * 0.85,
        transformOrigin: 'center',
      }}>
        <AKGlobe size={780} dotCount={1700}/>
      </div>
    </>
  );
}

// ── Intro 1: hero title — split layout ─────────────────────────────────────
function IntroScene1({ start, end }) {
  return (
    <Sprite start={start} end={end}>
      <SceneShell>
        <IntroScene1Inner/>
      </SceneShell>
    </Sprite>
  );
}

function IntroScene1Inner() {
  return (
    <>
      {/* Soft editorial background */}
      <BrandSoftBG opacity={1} intensity={0.18}/>

      {/* Big bold display — single long sentence, mask-revealed word by word */}
      <MaskTitle
        x={96} y={260}
        text="Proteja sua empresa com soluções de gerenciamento de identidade, acesso privilegiado, Zero Trust, segurança de IA e resiliência cibernética."
        size={78} weight={900} delay={0.3} perWord={0.05}
        maxWidth={1728} lineHeight={1.12}
      />
    </>
  );
}

// ── Intro 2: manifesto — two columns + numerical pillars ───────────────────
function IntroScene2({ start, end }) {
  return (
    <Sprite start={start} end={end}>
      <SceneShell>
        <IntroScene2Inner/>
      </SceneShell>
    </Sprite>
  );
}

function IntroScene2Inner() {
  return (
    <>
      {/* Soft editorial background */}
      <BrandSoftBG opacity={1} intensity={0.16}/>

      {/* Big asymmetric headline */}
      <MaskTitle
        x={96} y={300}
        text="Soluções líderes."
        size={150} weight={900} delay={0.25}
        maxWidth={1728} lineHeight={1.05}
      />
      <MaskTitle
        x={96} y={470}
        text="Equipa Certificada."
        size={150} weight={900} delay={0.45}
        color={AK.red}
        maxWidth={1728} lineHeight={1.05}
      />
    </>
  );
}

function StatStrip() {
  const { localTime } = useSprite();
  const items = [
    { num: '20+', label: 'anos de experiência' },
    { num: '10', label: 'parceiros líderes' },
    { num: '04', label: 'serviços geridos' },
    { num: 'PT', label: 'presença local' },
  ];
  return (
    <div style={{
      position: 'absolute',
      left: 96, right: 96, bottom: 80,
      display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)',
      borderTop: `2px solid ${AK.ink}`,
      paddingTop: 22,
    }}>
      {items.map((it, i) => {
        const t = clamp((localTime - i * 0.14) / 0.55, 0, 1);
        const e = Easing.easeOutCubic(t);
        return (
          <div key={i} style={{
            opacity: e, transform: `translateY(${(1-e) * 10}px)`,
            display: 'flex', flexDirection: 'column', gap: 6,
            borderRight: i < 3 ? `1px solid ${AK.rule}` : 'none',
            paddingRight: 24,
          }}>
            <div style={{
              fontFamily: AK.display, fontWeight: 900,
              fontSize: 76, color: AK.ink,
              lineHeight: 0.95, letterSpacing: '-0.04em',
            }}>{it.num}</div>
            <div style={{
              fontFamily: AK.display, fontSize: 16,
              fontWeight: 600, color: AK.gray2,
              letterSpacing: '-0.005em',
            }}>{it.label}</div>
          </div>
        );
      })}
    </div>
  );
}

// ── Transition scene — single line phrase, centered ─────────────────────────
function TransitionScene({ start, end, phrase, color }) {
  return (
    <Sprite start={start} end={end}>
      <SceneShell>
        <TransitionInner phrase={phrase} color={color}/>
      </SceneShell>
    </Sprite>
  );
}

function TransitionInner({ phrase, color }) {
  const { localTime, duration } = useSprite();
  // Line animates from left (0) to right (1) over the FULL scene duration,
  // ending exactly at the cut for a sense of continuity into the next scene.
  const lineT = clamp(localTime / Math.max(duration, 0.001), 0, 1);
  const lineE = Easing.easeInOutCubic(lineT);
  return (
    <>
      {/* Soft editorial background */}
      <BrandSoftBG opacity={1} intensity={0.14}/>

      {/* Phrase — left aligned */}
      <MaskTitle
        x={160} y={460}
        text={phrase}
        size={88} weight={900} delay={0.3} perWord={0.07}
        maxWidth={1600} lineHeight={1.15}
        color={color || AK.ink}
      />

      {/* Red rule sweeping left → right at the speed of the scene */}
      <div style={{
        position: 'absolute',
        left: 160, top: 700,
        width: 1600 * lineE, height: 4,
        background: AK.red,
        boxShadow: `0 0 18px ${AK.red}55`,
      }}/>
    </>
  );
}

// ── 25 de Abril Bridge — vector silhouette background ──────────────────────
function Bridge25Abril({ opacity = 1, color }) {
  const c = color || AK.red;
  // Iconic suspension bridge: two tall H-shaped towers, sweeping main suspension cables,
  // vertical hangers, and a deck spanning the harbour. Stylised, single-color.
  return (
    <svg
      viewBox="0 0 1920 1080"
      preserveAspectRatio="xMidYMid slice"
      style={{
        position: 'absolute', inset: 0,
        width: '100%', height: '100%',
        opacity,
        pointerEvents: 'none',
      }}
    >
      <defs>
        <linearGradient id="bridgeFade" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={c} stopOpacity="0.55"/>
          <stop offset="55%" stopColor={c} stopOpacity="0.85"/>
          <stop offset="100%" stopColor={c} stopOpacity="0.35"/>
        </linearGradient>
        <linearGradient id="waterFade" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={c} stopOpacity="0.18"/>
          <stop offset="100%" stopColor={c} stopOpacity="0"/>
        </linearGradient>
      </defs>

      {/* Distant water reflection lines */}
      <g opacity="0.35" stroke={c} strokeWidth="1.2" fill="none">
        <line x1="0"    y1="900"  x2="1920" y2="900"/>
        <line x1="0"    y1="930"  x2="1920" y2="930"/>
        <line x1="200"  y1="960"  x2="1720" y2="960"/>
        <line x1="380"  y1="990"  x2="1540" y2="990"/>
      </g>

      {/* Main bridge group */}
      <g stroke="url(#bridgeFade)" fill="none" strokeLinecap="round">

        {/* Roadway / deck — two parallel horizontals (truss feel) */}
        <line x1="60"   y1="720" x2="1860" y2="720" strokeWidth="6"/>
        <line x1="60"   y1="752" x2="1860" y2="752" strokeWidth="3"/>

        {/* Truss diagonals on the deck (light hatch) */}
        <g strokeWidth="1.2" opacity="0.6">
          {Array.from({length: 36}).map((_, i) => {
            const x = 80 + i * 50;
            return <line key={'tA'+i} x1={x} y1="722" x2={x+24} y2="750"/>;
          })}
          {Array.from({length: 36}).map((_, i) => {
            const x = 104 + i * 50;
            return <line key={'tB'+i} x1={x} y1="750" x2={x+24} y2="722"/>;
          })}
        </g>

        {/* Left tower — H-shape */}
        <g strokeWidth="7">
          <line x1="430"  y1="280" x2="430"  y2="760"/>
          <line x1="510"  y1="280" x2="510"  y2="760"/>
          {/* Cross beams */}
          <line x1="426"  y1="320" x2="514"  y2="320" strokeWidth="6"/>
          <line x1="426"  y1="430" x2="514"  y2="430" strokeWidth="6"/>
          <line x1="426"  y1="555" x2="514"  y2="555" strokeWidth="6"/>
          {/* Tower top finial */}
          <line x1="465"  y1="240" x2="465"  y2="280" strokeWidth="3"/>
          <line x1="475"  y1="240" x2="475"  y2="280" strokeWidth="3"/>
        </g>

        {/* Right tower — H-shape */}
        <g strokeWidth="7">
          <line x1="1410" y1="280" x2="1410" y2="760"/>
          <line x1="1490" y1="280" x2="1490" y2="760"/>
          <line x1="1406" y1="320" x2="1494" y2="320" strokeWidth="6"/>
          <line x1="1406" y1="430" x2="1494" y2="430" strokeWidth="6"/>
          <line x1="1406" y1="555" x2="1494" y2="555" strokeWidth="6"/>
          <line x1="1445" y1="240" x2="1445" y2="280" strokeWidth="3"/>
          <line x1="1455" y1="240" x2="1455" y2="280" strokeWidth="3"/>
        </g>

        {/* Main suspension cables — three sweeping curves between towers */}
        <path d="M 60 540 Q 250 360 470 290 Q 960 700 1450 290 Q 1670 360 1860 540"
          strokeWidth="5"/>
        {/* Backstays — outer cables anchoring beyond towers */}
        <path d="M 60 540 L 470 290" strokeWidth="3" opacity="0.85"/>
        <path d="M 1450 290 L 1860 540" strokeWidth="3" opacity="0.85"/>

        {/* Vertical hangers — between the main cable and the deck */}
        <g strokeWidth="1.4" opacity="0.85">
          {Array.from({length: 22}).map((_, i) => {
            const x = 480 + i * 44;
            // approximate parabola y = a(x-cx)^2 + miny
            const cx = 960;
            const a = (720 - 290) / Math.pow(960 - 470, 2);
            const y = 720 - a * Math.pow(x - cx, 2);
            return <line key={'h'+i} x1={x} y1={y} x2={x} y2="720"/>;
          })}
          {/* Side hangers — left of left tower */}
          {Array.from({length: 7}).map((_, i) => {
            const x = 110 + i * 50;
            // backstay line slope from (60,540) to (470,290)
            const m = (290 - 540) / (470 - 60);
            const y = 540 + m * (x - 60);
            return <line key={'hl'+i} x1={x} y1={y} x2={x} y2="720"/>;
          })}
          {/* Side hangers — right of right tower */}
          {Array.from({length: 7}).map((_, i) => {
            const x = 1500 + i * 50;
            const m = (540 - 290) / (1860 - 1450);
            const y = 290 + m * (x - 1450);
            return <line key={'hr'+i} x1={x} y1={y} x2={x} y2="720"/>;
          })}
        </g>

        {/* Tower bases / piers below the deck */}
        <g strokeWidth="5" opacity="0.85">
          <line x1="430"  y1="752" x2="430"  y2="860"/>
          <line x1="510"  y1="752" x2="510"  y2="860"/>
          <line x1="420"  y1="860" x2="520"  y2="860"/>
          <line x1="1410" y1="752" x2="1410" y2="860"/>
          <line x1="1490" y1="752" x2="1490" y2="860"/>
          <line x1="1400" y1="860" x2="1500" y2="860"/>
        </g>
      </g>

      {/* Soft water reflection of cables under deck */}
      <g opacity="0.20" stroke={c} fill="none">
        <path d="M 60 800 Q 250 880 470 920 Q 960 720 1450 920 Q 1670 880 1860 800"
          strokeWidth="2"/>
      </g>

      {/* Water glow */}
      <rect x="0" y="780" width="1920" height="300" fill="url(#waterFade)"/>
    </svg>
  );
}

window.Bridge25Abril = Bridge25Abril;
function ClosingScene({ start, end }) {
  return (
    <Sprite start={start} end={end}>
      <SceneShell>
        <ClosingInner/>
      </SceneShell>
    </Sprite>
  );
}

function ClosingInner() {
  const { localTime, duration } = useSprite();
  const bgT = clamp(localTime / 0.8, 0, 1);
  const bgE = Easing.easeOutCubic(bgT);

  return (
    <>
      {/* AK Dotted Surface — animated dot wave background */}
      <div style={{
        position: 'absolute', inset: 0,
        opacity: bgE,
        overflow: 'hidden',
        pointerEvents: 'none',
      }}>
        <AKDottedSurface
          width={1920} height={1080}
          cols={80} rows={70}
          spacing={26} amplitude={32}
          speed={0.5}
        />
      </div>

      {/* Soft paper veil for foreground readability */}
      <div style={{
        position: 'absolute', inset: 0,
        background: `linear-gradient(180deg, ${AK.paper}E6 0%, ${AK.paper}99 35%, ${AK.paper}66 70%, ${AK.paper}DD 100%)`,
        pointerEvents: 'none',
      }}/>

      {/* Top-left: AK logo */}
      <div style={{
        position: 'absolute', left: 96, top: 96,
        opacity: clamp(localTime / 0.6, 0, 1),
      }}>
        <img src="assets/logos/ak-networks.png" alt="AK Networks"
          style={{ height: 84, width: 'auto', display: 'block' }}
        />
      </div>

      {/* Closing phrase */}
      <MaskTitle
        x={96} y={360}
        text="A construir pontes para o futuro da cibersegurança em Portugal."
        size={108} weight={900}
        delay={0.4} perWord={0.07}
        maxWidth={1728} lineHeight={1.08}
        highlight="cibersegurança"
      />

      {/* Bottom row */}
      <div style={{
        position: 'absolute', left: 96, right: 96, bottom: 96,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        borderTop: `2px solid ${AK.ink}`, paddingTop: 36,
      }}>
        <div style={{
          opacity: clamp((localTime - 1.6) / 0.6, 0, 1),
          display: 'flex', alignItems: 'center', gap: 18,
        }}>
          {/* Globe icon */}
          <svg width="40" height="40" viewBox="0 0 40 40" style={{display:'block'}}>
            <circle cx="20" cy="20" r="17" fill="none" stroke={AK.ink} strokeWidth="1.6"/>
            <ellipse cx="20" cy="20" rx="7" ry="17" fill="none" stroke={AK.ink} strokeWidth="1.4"/>
            <line x1="3" y1="20" x2="37" y2="20" stroke={AK.ink} strokeWidth="1.4"/>
            <path d="M5 12 Q20 16 35 12" fill="none" stroke={AK.ink} strokeWidth="1.2"/>
            <path d="M5 28 Q20 24 35 28" fill="none" stroke={AK.ink} strokeWidth="1.2"/>
          </svg>
          <div style={{
            fontFamily: AK.display, fontWeight: 500,
            fontSize: 38, color: AK.ink, letterSpacing: '-0.01em',
          }}>www.aknetworks.pt</div>
        </div>

        <div style={{
          opacity: clamp((localTime - 2.0) / 0.6, 0, 1),
          display: 'flex', gap: 64, alignItems: 'center',
        }}>
          {[
            { name: 'Brasil',         flag: (
              <svg width="80" height="56" viewBox="0 0 80 56" style={{display:'block'}}>
                <rect width="80" height="56" fill="#009C3B"/>
                <polygon points="40,6 73,28 40,50 7,28" fill="#FFDF00"/>
                <circle cx="40" cy="28" r="11" fill="#002776"/>
                <path d="M29 28 Q40 22 51 28" stroke="#FFFFFF" strokeWidth="2" fill="none"/>
              </svg>
            )},
            { name: 'Portugal',       flag: (
              <svg width="80" height="56" viewBox="0 0 80 56" style={{display:'block'}}>
                <rect width="34" height="56" fill="#006633"/>
                <rect x="34" width="46" height="56" fill="#D52B1E"/>
                <circle cx="34" cy="28" r="10" fill="#FFCC00" stroke="#000000" strokeWidth="0.8"/>
                <circle cx="34" cy="28" r="6" fill="#D52B1E"/>
              </svg>
            )},
            { name: 'Estados Unidos', flag: (
              <svg width="80" height="56" viewBox="0 0 80 56" style={{display:'block'}}>
                <rect width="80" height="56" fill="#FFFFFF"/>
                {[0,2,4,6,8,10,12].map(i => (
                  <rect key={i} y={i*4.3} width="80" height="4.3" fill="#B22234"/>
                ))}
                <rect width="34" height="30" fill="#3C3B6E"/>
              </svg>
            )},
          ].map((c, i) => (
            <div key={i} style={{
              display: 'flex', alignItems: 'center', gap: 14,
            }}>
              <div style={{
                fontFamily: AK.display, fontSize: 28, fontWeight: 600,
                color: AK.ink, letterSpacing: '-0.01em',
              }}>{c.name}</div>
              <div style={{ boxShadow: '0 0 0 1px rgba(10,10,10,0.12)' }}>{c.flag}</div>
            </div>
          ))}
        </div>
      </div>
    </>
  );
}

Object.assign(window, {
  ColdOpenScene, IntroScene1, IntroScene2,
  TransitionScene, ClosingScene,
});
