/**
 * FlashSale Landing Page - Animations, Parallax & Counters
 */
document.addEventListener('DOMContentLoaded', () => {
    // 1. Hero Ken Burns / Parallax effect on scroll
    const heroBg = document.querySelector('.landing-hero__bg');
    if (heroBg) {
        window.addEventListener('scroll', () => {
            const scrolled = window.scrollY;
            if (scrolled < 1000) {
                heroBg.style.transform = `scale(1.05) translateY(${scrolled * 0.35}px)`;
            }
        }, { passive: true });
    }

    // 2. Animated Stats Counter
    const statCounters = document.querySelectorAll('.stat-counter__value');
    let counted = false;

    function countUp(el) {
        const target = parseInt(el.getAttribute('data-target') || '0', 10);
        const prefix = el.getAttribute('data-prefix') || '';
        const suffix = el.getAttribute('data-suffix') || '';
        const duration = 2000;
        const steps = 60;
        const stepTime = duration / steps;
        let current = 0;

        const timer = setInterval(() => {
            current += Math.ceil(target / steps);
            if (current >= target) {
                current = target;
                clearInterval(timer);
            }
            el.textContent = `${prefix}${current.toLocaleString()}${suffix}`;
        }, stepTime);
    }

    // IntersectionObserver for Stats Counter
    const statsSection = document.querySelector('.stats-counter');
    if (statsSection && statCounters.length > 0) {
        const statsObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting && !counted) {
                    counted = true;
                    statCounters.forEach(counter => countUp(counter));
                    statsObserver.unobserve(entry.target);
                }
            });
        }, { threshold: 0.3 });
        statsObserver.observe(statsSection);
    }

    // 3. Staggered Scroll Reveal Observer
    const revealElements = document.querySelectorAll('.reveal');
    if (revealElements.length > 0) {
        const revealObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('is-visible');
                    revealObserver.unobserve(entry.target);
                }
            });
        }, { threshold: 0.15, rootMargin: '0px 0px -50px 0px' });

        revealElements.forEach(el => revealObserver.observe(el));
    }

    // 4. Navbar Scroll Glassmorphism Transition
    const navbar = document.getElementById('navbar');
    if (navbar) {
        window.addEventListener('scroll', () => {
            if (window.scrollY > 50) {
                navbar.classList.add('navbar--scrolled');
            } else {
                navbar.classList.remove('navbar--scrolled');
            }
        }, { passive: true });
    }
});
