/**
 * ALGA ARENA — Section Timelines
 * Hooks scroll offsets to camera path points and core geometry transitions
 */

'use strict';

window.CinematicTimelines = (() => {
    const init = (camera, scene) => {
        if (typeof gsap === 'undefined' || typeof ScrollTrigger === 'undefined') return;

        gsap.registerPlugin(ScrollTrigger);

        const fragments = window.CinematicFragments?.getFragments() || [];
        const coreGroup = window.CinematicCore?.getCoreGroup();
        const centralCore = window.CinematicCore?.getCentralCore();
        const outerRings = window.CinematicCore?.getOuterRings() || [];

        // 1. Hero timeline: assemble core from floating fragments
        const tl = gsap.timeline({
            scrollTrigger: {
                trigger: ".landing-hero",
                start: "top top",
                end: "bottom top",
                scrub: 1.2
            }
        });

        fragments.forEach(frag => {
            tl.to(frag.position, {
                x: frag.userData.targetPos.x,
                y: frag.userData.targetPos.y,
                z: frag.userData.targetPos.z,
                duration: 1.5,
                ease: "power2.out"
            }, 0);
        });

        tl.to(camera.position, {
            z: 8,
            duration: 2.0,
            ease: "sine.inOut"
        }, 0);

        if (coreGroup) {
            tl.to(coreGroup.rotation, {
                y: Math.PI * 1.5,
                duration: 3,
                ease: "none"
            }, 0);
        }

        // 2. Recruitment timeline (node connections)
        ScrollTrigger.create({
            trigger: "[aria-labelledby='recruitment-heading']",
            start: "top 80%",
            end: "bottom 20%",
            scrub: true,
            onEnter: () => {
                if (centralCore) gsap.to(centralCore.scale, { x: 1.1, y: 1.1, z: 1.1, duration: 0.6 });
                outerRings.forEach(ring => {
                    gsap.to(ring.scale, { x: 1.2, y: 1.2, z: 1.2, duration: 0.6 });
                });
            },
            onLeaveBack: () => {
                if (centralCore) gsap.to(centralCore.scale, { x: 1, y: 1, z: 1, duration: 0.6 });
                outerRings.forEach(ring => {
                    gsap.to(ring.scale, { x: 1, y: 1, z: 1, duration: 0.6 });
                });
            }
        });

        // 3. Tournaments timeline (lights tinting)
        ScrollTrigger.create({
            trigger: "[aria-labelledby='tournaments-heading']",
            start: "top 80%",
            end: "bottom 20%",
            onEnter: () => {
                gsap.to(camera.position, { z: 6.8, duration: 1.2 });
                const rim = window.CinematicLighting?.getRimLight();
                if (rim) rim.color.setHex(0x7c5cfc); // purple glow
            },
            onLeaveBack: () => {
                gsap.to(camera.position, { z: 8, duration: 1.2 });
                const rim = window.CinematicLighting?.getRimLight();
                if (rim) rim.color.setHex(0x00d4ff); // cyan glow
            }
        });
    };

    return { init };
})();
