/*!
 * NovaPure – main.js
 * Handles: header scroll, cookie banner, FAQ accordion, mobile nav
 */

(function () {
  'use strict';

  /* ============================
     HEADER: scroll effect
  ============================ */
  var header = document.getElementById('siteHeader');
  if (header) {
    function onScroll() {
      if (window.scrollY > 48) {
        header.classList.add('is-scrolled');
      } else {
        header.classList.remove('is-scrolled');
      }
    }
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
  }

  /* HERO: load class (kept for future use) */

  /* ============================
     MOBILE NAV TOGGLE
  ============================ */
  var navToggle = document.getElementById('navToggle');
  var mainNav   = document.getElementById('mainNav');

  if (navToggle && mainNav) {
    navToggle.addEventListener('click', function () {
      var isOpen = mainNav.classList.toggle('is-open');
      navToggle.classList.toggle('is-active', isOpen);
      navToggle.setAttribute('aria-expanded', String(isOpen));
    });

    // Close nav on link click
    mainNav.querySelectorAll('.site-header__nav-link').forEach(function (link) {
      link.addEventListener('click', function () {
        mainNav.classList.remove('is-open');
        navToggle.classList.remove('is-active');
        navToggle.setAttribute('aria-expanded', 'false');
      });
    });
  }

  /* ============================
     COOKIE BANNER
  ============================ */
  var COOKIE_KEY = 'np_cookie_consent';
  var cookieBar  = document.getElementById('cookieBar');

  function setCookieConsent(val) {
    try {
      sessionStorage.setItem(COOKIE_KEY, val);
    } catch (e) { /* silently fail in privacy mode */ }
  }

  function getCookieConsent() {
    try {
      return sessionStorage.getItem(COOKIE_KEY);
    } catch (e) { return null; }
  }

  if (cookieBar && !getCookieConsent()) {
    // Show after small delay
    setTimeout(function () {
      cookieBar.classList.add('is-visible');
    }, 800);

    var btnAccept = document.getElementById('cookieAccept');
    var btnReject = document.getElementById('cookieReject');

    function hideBanner() {
      cookieBar.classList.remove('is-visible');
      setTimeout(function () {
        cookieBar.remove();
      }, 500);
    }

    if (btnAccept) {
      btnAccept.addEventListener('click', function () {
        setCookieConsent('accepted');
        hideBanner();
      });
    }
    if (btnReject) {
      btnReject.addEventListener('click', function () {
        setCookieConsent('rejected');
        hideBanner();
      });
    }
  } else if (cookieBar) {
    cookieBar.remove();
  }

  /* ============================
     FAQ ACCORDION
  ============================ */
  var faqItems = document.querySelectorAll('.faq-item');

  faqItems.forEach(function (item) {
    var trigger = item.querySelector('.faq-item__trigger');
    var body    = item.querySelector('.faq-item__body');

    if (!trigger || !body) return;

    trigger.addEventListener('click', function () {
      var isOpen = item.classList.contains('is-open');

      // Close all others
      faqItems.forEach(function (other) {
        if (other !== item && other.classList.contains('is-open')) {
          other.classList.remove('is-open');
          other.querySelector('.faq-item__trigger').setAttribute('aria-expanded', 'false');
          var ob = other.querySelector('.faq-item__body');
          if (ob) ob.hidden = true;
        }
      });

      // Toggle current
      if (isOpen) {
        item.classList.remove('is-open');
        trigger.setAttribute('aria-expanded', 'false');
        body.hidden = true;
      } else {
        item.classList.add('is-open');
        trigger.setAttribute('aria-expanded', 'true');
        body.hidden = false;
      }
    });
  });

  /* ============================
     SMOOTH SCROLL for anchors
  ============================ */
  document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
    anchor.addEventListener('click', function (e) {
      var href = anchor.getAttribute('href');
      if (href === '#') return;
      var target = document.querySelector(href);
      if (target) {
        e.preventDefault();
        var headerH = header ? header.offsetHeight : 72;
        var top     = target.getBoundingClientRect().top + window.scrollY - headerH - 12;
        window.scrollTo({ top: top, behavior: 'smooth' });
      }
    });
  });

  /* ============================
     ORDER FORM – basic validation
  ============================ */
  var orderForm = document.getElementById('orderForm');
  if (orderForm) {
    orderForm.addEventListener('submit', function (e) {
      var required = orderForm.querySelectorAll('[required]');
      var valid = true;
      required.forEach(function (field) {
        field.style.borderColor = '';
        if (!field.value.trim()) {
          field.style.borderColor = '#ef4444';
          valid = false;
        }
        if (field.type === 'email' && field.value.trim()) {
          var emailRx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
          if (!emailRx.test(field.value.trim())) {
            field.style.borderColor = '#ef4444';
            valid = false;
          }
        }
        if (field.type === 'checkbox' && !field.checked) {
          valid = false;
        }
      });
      if (!valid) {
        e.preventDefault();
        var firstError = orderForm.querySelector('[style*="border-color: rgb(239, 68, 68)"]');
        if (firstError) {
          firstError.focus();
          firstError.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
      }
    });
  }

  /* ============================
     SECTION ENTRANCE ANIMATION
  ============================ */
  if ('IntersectionObserver' in window) {
    var sections = document.querySelectorAll('.whom-block, .product-overview, .lifestyle-section, .social-proof, .checkout-section, .faq-section');
    var observer = new IntersectionObserver(function (entries) {
      entries.forEach(function (entry) {
        if (entry.isIntersecting) {
          entry.target.style.opacity   = '1';
          entry.target.style.transform = 'translateY(0)';
          observer.unobserve(entry.target);
        }
      });
    }, { threshold: 0.08 });

    sections.forEach(function (s) {
      s.style.opacity   = '0';
      s.style.transform = 'translateY(28px)';
      s.style.transition = 'opacity 0.55s ease, transform 0.55s ease';
      observer.observe(s);
    });
  }

})();
