import Lenis from 'lenis';

document.addEventListener('DOMContentLoaded', () => {
  // 1. Initialize Smooth Scrolling
  const lenis = new Lenis();
  function raf(time) {
    lenis.raf(time);
    requestAnimationFrame(raf);
  }
  requestAnimationFrame(raf);

  // 2. Mobile Menu Toggle
  const navToggle = document.querySelector('.nav-toggle');
  const navList = document.querySelector('.nav-list');
  
  if (navToggle && navList) {
    navToggle.addEventListener('click', () => {
      navList.classList.toggle('active');
      navToggle.classList.toggle('active');
    });

    // Close menu when clicking a link
    navList.querySelectorAll('a').forEach(link => {
      link.addEventListener('click', () => {
        navList.classList.remove('active');
        navToggle.classList.remove('active');
      });
    });
  }

  // 3. Cookie Consent Logic
  const cookieBar = document.getElementById('cookieBar');
  const acceptBtn = document.getElementById('cookieAccept');
  const rejectBtn = document.getElementById('cookieReject');

  if (cookieBar) {
    const hasConsent = localStorage.getItem('vidica-cookie-consent');
    if (!hasConsent) {
      setTimeout(() => {
        cookieBar.classList.add('active');
      }, 2000);
    }

    acceptBtn?.addEventListener('click', () => {
      localStorage.setItem('vidica-cookie-consent', 'accepted');
      cookieBar.classList.remove('active');
    });

    rejectBtn?.addEventListener('click', () => {
      localStorage.setItem('vidica-cookie-consent', 'rejected');
      cookieBar.classList.remove('active');
    });
  }

  // 4. Update Footer Year
  const yearSpan = document.getElementById('year');
  if (yearSpan) {
    yearSpan.textContent = new Date().getFullYear();
  }

  // 5. Intersection Observer for Scroll Animations (Simple)
  const observerOptions = {
    threshold: 0.1
  };

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add('in-view');
        observer.unobserve(entry.target);
      }
    });
  }, observerOptions);

  document.querySelectorAll('.card, .hero-text, .hero-image, .section-with-image').forEach(el => {
    observer.observe(el);
  });
});