// main.js - Client side animations and interactivity

document.addEventListener('DOMContentLoaded', function() {
    // Add animate.css classes to elements when they enter the viewport
    const observerOptions = {
        threshold: 0.1
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const animationClass = entry.target.dataset.animation;
                if (animationClass) {
                    entry.target.classList.add('animate__animated', animationClass);
                }
            }
        });
    }, observerOptions);

    document.querySelectorAll('.animate-on-scroll').forEach(el => {
        observer.observe(el);
    });

    // Sidebar Toggle
    const sidebarToggle = document.getElementById('sidebarToggle');
    const sidebar = document.getElementById('sidebar');
    const contentWrapper = document.querySelector('.content-wrapper');

    // Enable tooltips (for sidebar items)
    if (window.bootstrap) {
        const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
        tooltipTriggerList.forEach(el => new bootstrap.Tooltip(el));
    }

    if (sidebarToggle) {
        sidebarToggle.addEventListener('click', function(e) {
            e.stopPropagation(); // Prevent immediate closing
            if (window.innerWidth > 991) {
                // Desktop toggle
                sidebar.classList.toggle('collapsed');
                contentWrapper.classList.toggle('full-width');
            } else {
                // Mobile toggle
                sidebar.classList.toggle('active');
            }
        });
    }

    // Close sidebar on mobile when content or overlay is clicked
    const sidebarOverlay = document.getElementById('sidebarOverlay');
    if (contentWrapper) {
        contentWrapper.addEventListener('click', function() {
            if (window.innerWidth <= 991 && sidebar.classList.contains('active')) {
                sidebar.classList.remove('active');
            }
        });
    }

    if (sidebarOverlay) {
        sidebarOverlay.addEventListener('click', function() {
            sidebar.classList.remove('active');
        });
    }

    // Back to Top functionality
    const backToTopBtn = document.getElementById('backToTop');
    
    if (backToTopBtn) {
        window.addEventListener('scroll', function() {
            if (window.scrollY > 300) {
                if (backToTopBtn.classList.contains('d-none')) {
                    backToTopBtn.classList.remove('d-none');
                    backToTopBtn.classList.add('fade-in');
                }
            } else {
                backToTopBtn.classList.add('d-none');
                backToTopBtn.classList.remove('fade-in');
            }
        });

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