document.addEventListener('DOMContentLoaded', function() {
    initHeroSlider();
    initFacilitiesCarousel();
    initMobileNav();
    initScrollAnimations();
    initSmoothScroll();
    initBackToTop();
    initCounterAnimation();
    initHeaderScroll();
});

function initHeroSlider() {
    const track = document.querySelector('.slider-track');
    if (!track) return;

    const slides = track.querySelectorAll('.slide');
    const dots = document.querySelectorAll('.slider-dot');
    let current = 0;
    const total = slides.length;

    function goToSlide(index) {
        current = index;
        if (current >= total) current = 0;
        if (current < 0) current = total - 1;
        track.style.transform = `translateX(-${current * 100}%)`;
        dots.forEach((dot, i) => {
            dot.classList.toggle('active', i === current);
        });
    }

    const prevBtn = document.querySelector('.slider-btn.prev');
    const nextBtn = document.querySelector('.slider-btn.next');

    if (prevBtn) prevBtn.addEventListener('click', () => goToSlide(current - 1));
    if (nextBtn) nextBtn.addEventListener('click', () => goToSlide(current + 1));

    dots.forEach((dot, i) => {
        dot.addEventListener('click', () => goToSlide(i));
    });

    setInterval(() => goToSlide(current + 1), 5000);
}

function initFacilitiesCarousel() {
    const track = document.querySelector('.facilities-track');
    if (!track) return;

    const items = track.querySelectorAll('.facility-item');
    let offset = 0;
    const itemWidth = 260;
    const visibleWidth = track.parentElement.clientWidth;
    const totalWidth = items.length * itemWidth;
    const maxOffset = Math.max(0, totalWidth - visibleWidth);

    const prevBtn = document.querySelector('.facilities-prev');
    const nextBtn = document.querySelector('.facilities-next');

    if (prevBtn) {
        prevBtn.addEventListener('click', function() {
            offset = Math.max(0, offset - itemWidth * 2);
            track.style.transform = `translateX(-${offset}px)`;
        });
    }

    if (nextBtn) {
        nextBtn.addEventListener('click', function() {
            offset = Math.min(maxOffset, offset + itemWidth * 2);
            track.style.transform = `translateX(-${offset}px)`;
        });
    }
}

function initMobileNav() {
    const toggle = document.querySelector('.nav-toggle');
    const navList = document.querySelector('.main-nav ul');
    const backdrop = document.querySelector('.nav-backdrop');

    if (toggle && navList) {
        toggle.addEventListener('click', function() {
            navList.classList.toggle('active');
            if (backdrop) backdrop.classList.toggle('active');
            document.body.style.overflow = navList.classList.contains('active') ? 'hidden' : '';

            const icon = toggle.querySelector('i');
            if (icon) {
                icon.classList.toggle('fa-bars');
                icon.classList.toggle('fa-times');
            }
        });

        if (backdrop) {
            backdrop.addEventListener('click', function() {
                navList.classList.remove('active');
                backdrop.classList.remove('active');
                document.body.style.overflow = '';
                const icon = toggle.querySelector('i');
                if (icon) {
                    icon.classList.remove('fa-times');
                    icon.classList.add('fa-bars');
                }
            });
        }

        navList.querySelectorAll('a').forEach(function(link) {
            link.addEventListener('click', function() {
                navList.classList.remove('active');
                if (backdrop) backdrop.classList.remove('active');
                document.body.style.overflow = '';
                const icon = toggle.querySelector('i');
                if (icon) {
                    icon.classList.remove('fa-times');
                    icon.classList.add('fa-bars');
                }
            });
        });
    }
}

function initScrollAnimations() {
    const animatedElements = document.querySelectorAll('.fade-in-up, .fade-in-left, .fade-in-right, .fade-in, .scale-in');
    if (!animatedElements.length) return;

    const observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                entry.target.classList.add('visible');
                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.1,
        rootMargin: '0px 0px -40px 0px'
    });

    animatedElements.forEach(function(el) {
        observer.observe(el);
    });
}

function initSmoothScroll() {
    document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
        anchor.addEventListener('click', function(e) {
            var targetId = this.getAttribute('href');
            if (targetId === '#') return;

            var target = document.querySelector(targetId);
            if (target) {
                e.preventDefault();
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
            }
        });
    });
}

function initBackToTop() {
    var btn = document.querySelector('.back-to-top');
    if (!btn) return;

    window.addEventListener('scroll', function() {
        if (window.pageYOffset > 400) {
            btn.classList.add('visible');
        } else {
            btn.classList.remove('visible');
        }
    });

    btn.addEventListener('click', function() {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
}

function initCounterAnimation() {
    var counters = document.querySelectorAll('.stat-number');
    if (!counters.length) return;

    var observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                var target = entry.target;
                var finalValue = parseInt(target.getAttribute('data-count') || target.textContent, 10);
                if (isNaN(finalValue)) return;

                var suffix = target.textContent.replace(/[0-9]/g, '');
                var duration = 2000;
                var startTime = null;

                function animate(currentTime) {
                    if (!startTime) startTime = currentTime;
                    var progress = Math.min((currentTime - startTime) / duration, 1);
                    var eased = 1 - Math.pow(1 - progress, 3);
                    target.textContent = Math.floor(eased * finalValue) + suffix;
                    if (progress < 1) {
                        requestAnimationFrame(animate);
                    }
                }

                requestAnimationFrame(animate);
                observer.unobserve(target);
            }
        });
    }, { threshold: 0.5 });

    counters.forEach(function(counter) {
        observer.observe(counter);
    });
}

function initHeaderScroll() {
    var header = document.querySelector('.main-header');
    if (!header) return;

    window.addEventListener('scroll', function() {
        if (window.pageYOffset > 50) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }
    });
}
