// script.js
document.addEventListener('DOMContentLoaded', () => {

    // Mobile Navigation Toggle
    const navToggle = document.querySelector('.nav-toggle');
    const mainNav = document.querySelector('#main-nav');

    if (navToggle && mainNav) {
        navToggle.addEventListener('click', () => {
            const isExpanded = navToggle.getAttribute('aria-expanded') === 'true' || false;
            navToggle.setAttribute('aria-expanded', !isExpanded);
            mainNav.classList.toggle('nav-active');
            // Optional: Toggle body scroll
            // document.body.classList.toggle('no-scroll-mobile');
        });
    }

    // Smooth scroll for navigation links (CSS `scroll-behavior: smooth` is primary)
    // This JS version can offer more control or fallback if needed,
    // but for "indispensable JS" it might be omitted if CSS is sufficient.
    // For now, relying on CSS scroll-behavior. If specific offsets or advanced
    // functionality were needed, this would be expanded.

    // Active Nav Link Highlighting on Scroll
    const sections = document.querySelectorAll('main section[id]');
    const navLinks = document.querySelectorAll('.site-nav__link');

    if (sections.length > 0 && navLinks.length > 0) {
        const observerOptions = {
            root: null, // relative to document viewport
            rootMargin: '-50% 0px -50% 0px', // Triggers when section is in middle of viewport
            threshold: 0 // or a small value like 0.01
        };

        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const id = entry.target.getAttribute('id');
                    navLinks.forEach(link => {
                        link.classList.remove('active');
                        if (link.getAttribute('href') === `#${id}`) {
                            link.classList.add('active');
                        }
                    });
                }
            });
        }, observerOptions);

        sections.forEach(section => {
            observer.observe(section);
        });
    }
    
    // Close mobile nav when a link is clicked
    if (mainNav) {
        const navMenuLinks = mainNav.querySelectorAll('a');
        navMenuLinks.forEach(link => {
            link.addEventListener('click', () => {
                if (mainNav.classList.contains('nav-active')) {
                    navToggle.setAttribute('aria-expanded', 'false');
                    mainNav.classList.remove('nav-active');
                }
            });
        });
    }


    // Swiper Initialization for Testimonials
    if (document.querySelector('.testimonial-slider')) {
        const testimonialSwiper = new Swiper('.testimonial-slider', {
            loop: true,
            slidesPerView: 1,
            spaceBetween: 30,
            // autoplay: {
            //     delay: 5000,
            //     disableOnInteraction: false,
            // },
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            breakpoints: {
                768: {
                    slidesPerView: 2,
                    spaceBetween: 20
                },
            }
        });
    }

    // FAQ Accordion: Native <details> element is used, so JS is not strictly needed for basic functionality.
    // If custom animations or behaviors were required, JS would be added here.

    // Contact Form Submission (Simple placeholder)
    const contactForm = document.getElementById('contact-form');
    if (contactForm) {
        contactForm.addEventListener('submit', function(event) {
            event.preventDefault();
            // Basic validation is handled by HTML5 `required` attribute.
            // For a real form, you'd add more robust validation and AJAX submission here.
            alert('Grazie per la tua richiesta! Ti risponderemo al più presto.');
            contactForm.reset(); // Clear the form
        });
    }
    
    // Newsletter Form (Simple placeholder)
    const newsletterForm = document.querySelector('.newsletter-form');
    if (newsletterForm) {
        newsletterForm.addEventListener('submit', function(event) {
            event.preventDefault();
            const emailInput = newsletterForm.querySelector('input[type="email"]');
            if (emailInput && emailInput.value) {
                 alert(`Grazie per esserti iscritto alla newsletter con l'email: ${emailInput.value}`);
                 newsletterForm.reset();
            } else {
                alert('Per favore, inserisci un indirizzo email valido.');
            }
        });
    }


    // Set current year in footer
    const yearSpan = document.getElementById('current-year');
    if (yearSpan) {
        yearSpan.textContent = new Date().getFullYear();
    }

});