// AOS Initialization
AOS.init({
  duration: 1200,
});

// Hero js
function initHeroSlider() {
  const slides = document.querySelectorAll(".hero-slider-item");
  const indicatorItems = document.querySelectorAll(
    ".hero-slider-indicator-item",
  );
  const nextBtn = document.getElementById("hero-slider-next");
  const prevBtn = document.getElementById("hero-slider-prev");

  if (!slides.length || !nextBtn || !prevBtn) return;

  let currentSlide = 0;
  const totalSlides = slides.length;
  let autoSlideInterval;

  function updateSlider(index) {
    slides.forEach((slide, i) => {
      if (i === index) {
        slide.style.opacity = "1";
        slide.style.zIndex = "10";
        slide.style.transform = "translateX(0)";
      } else {
        slide.style.opacity = "0";
        slide.style.zIndex = "0";
        slide.style.transform =
          i < index ? "translateX(-20px)" : "translateX(20px)";
      }
    });

    indicatorItems.forEach((indicator, i) => {
      const text = indicator.querySelector("span");
      const bar = indicator.querySelector(".hero-slider-progress-bar");

      if (i === index) {
        indicator.classList.remove("opacity-40");
        text.classList.add("text-lg");
        text.classList.remove("text-sm");
        if (bar) {
          bar.classList.add("h-12", "bg-primary");
          bar.classList.remove("h-8", "bg-white");
        }
      } else {
        indicator.classList.add("opacity-40");
        text.classList.remove("text-lg");
        text.classList.add("text-sm");
        if (bar) {
          bar.classList.remove("h-12", "bg-primary");
          bar.classList.add("h-8", "bg-white");
        }
      }
    });
    currentSlide = index;
  }

  function nextSlide() {
    let next = (currentSlide + 1) % totalSlides;
    updateSlider(next);
  }

  function prevSlide() {
    let prev = (currentSlide - 1 + totalSlides) % totalSlides;
    updateSlider(prev);
  }

  nextBtn.addEventListener("click", () => {
    nextSlide();
    resetAutoSlide();
  });

  prevBtn.addEventListener("click", () => {
    prevSlide();
    resetAutoSlide();
  });

  function startAutoSlide() {
    autoSlideInterval = setInterval(nextSlide, 8000);
  }

  function resetAutoSlide() {
    clearInterval(autoSlideInterval);
    startAutoSlide();
  }

  updateSlider(0);
  startAutoSlide();
}

document.addEventListener("DOMContentLoaded", () => {
  initHeroSlider();
});

// Services
function slide(direction) {
  const slider = document.getElementById("serviceSlider");
  const card = slider.querySelector(".prism-card");
  if (!card) return;

  // Single card increment logic: card width + the 16px gap
  const scrollAmount = card.offsetWidth + 16;

  if (direction === "left") {
    slider.scrollBy({ left: -scrollAmount, behavior: "smooth" });
  } else {
    slider.scrollBy({ left: scrollAmount, behavior: "smooth" });
  }
}

/**
 * Testimonial Slider Logic
 */
function initTestimonialSlider() {
  const state = {
    slider: document.getElementById("testimonialSlider"),
    nextBtn: document.getElementById("nextBtn"),
    prevBtn: document.getElementById("prevBtn"),
    autoPlayInterval: 6000,
    autoPlayTimer: null,
  };

  if (!state.slider || !state.nextBtn || !state.prevBtn) return;

  const getScrollStep = () => {
    const firstCard = state.slider.querySelector(".snap-center");
    // Cards have gap-6 (24px). Default to 344px if element check fails.
    return firstCard ? firstCard.offsetWidth + 24 : 344;
  };

  const scrollToNext = () => {
    const maxScroll = state.slider.scrollWidth - state.slider.clientWidth;
    if (state.slider.scrollLeft >= maxScroll - 10) {
      state.slider.scrollTo({ left: 0, behavior: "smooth" });
    } else {
      state.slider.scrollBy({ left: getScrollStep(), behavior: "smooth" });
    }
  };

  const scrollToPrev = () => {
    if (state.slider.scrollLeft <= 10) {
      state.slider.scrollTo({
        left: state.slider.scrollWidth,
        behavior: "smooth",
      });
    } else {
      state.slider.scrollBy({ left: -getScrollStep(), behavior: "smooth" });
    }
  };

  const startAutoPlay = () => {
    stopAutoPlay();
    state.autoPlayTimer = setInterval(scrollToNext, state.autoPlayInterval);
  };

  const stopAutoPlay = () => {
    if (state.autoPlayTimer) {
      clearInterval(state.autoPlayTimer);
      state.autoPlayTimer = null;
    }
  };

  // Event Listeners
  state.nextBtn.addEventListener("click", () => {
    scrollToNext();
    startAutoPlay(); // Reset timer on manual interaction
  });

  state.prevBtn.addEventListener("click", () => {
    scrollToPrev();
    startAutoPlay(); // Reset timer on manual interaction
  });

  state.slider.addEventListener("mouseenter", stopAutoPlay);
  state.slider.addEventListener("mouseleave", startAutoPlay);

  // Touch support pause
  state.slider.addEventListener("touchstart", stopAutoPlay, { passive: true });
  state.slider.addEventListener("touchend", startAutoPlay, { passive: true });

  // Initialize
  startAutoPlay();
}

// Initialize on DOM Content Loaded
document.addEventListener("DOMContentLoaded", initTestimonialSlider);

// navbar js
document.addEventListener("DOMContentLoaded", () => {
  // Desktop Elements
  const servicesTrigger = document.getElementById("dg-nav-services-trigger");
  const megaMenu = document.getElementById("dg-nav-mega-dropdown");
  const expandIcon = document.getElementById("dg-nav-expand-icon");

  // Mobile Elements
  const mobileToggle = document.getElementById("dg-nav-mobile-toggle");
  const mobileClose = document.getElementById("dg-nav-mobile-close");
  const mobileDrawer = document.getElementById("dg-nav-drawer");
  const overlay = document.getElementById("dg-nav-overlay");
  const mobileAccordionBtn = document.getElementById(
    "dg-nav-mobile-accordion-btn",
  );
  const mobileAccordion = document.getElementById("dg-nav-mobile-accordion");
  const mobileExpandIcon = document.getElementById("dg-nav-mobile-expand-icon");

  /**
   * State Handlers
   */
  const setMegaMenuState = (isActive) => {
    if (megaMenu) {
      megaMenu.classList.toggle("is-active", isActive);
    }
    if (expandIcon) {
      expandIcon.style.transform = isActive ? "rotate(180deg)" : "rotate(0deg)";
    }
    document.body.style.overflow = isActive ? "hidden" : "";
  };

  const setMobileDrawerState = (isActive) => {
    if (mobileDrawer) mobileDrawer.classList.toggle("is-active", isActive);
    if (overlay) overlay.classList.toggle("is-active", isActive);
    document.body.style.overflow = isActive ? "hidden" : "";
  };

  /**
   * Desktop Listeners
   */
  if (servicesTrigger && megaMenu) {
    const handleEnter = () => setMegaMenuState(true);
    const handleLeave = (e) => {
      const target = e.relatedTarget;
      if (!megaMenu.contains(target) && !servicesTrigger.contains(target)) {
        setMegaMenuState(false);
      }
    };

    servicesTrigger.addEventListener("mouseenter", handleEnter);
    servicesTrigger.addEventListener("mouseleave", handleLeave);
    megaMenu.addEventListener("mouseenter", handleEnter);
    megaMenu.addEventListener("mouseleave", handleLeave);
  }

  /**
   * Mobile Listeners
   */
  if (mobileToggle)
    mobileToggle.addEventListener("click", () => setMobileDrawerState(true));
  if (mobileClose)
    mobileClose.addEventListener("click", () => setMobileDrawerState(false));
  if (overlay)
    overlay.addEventListener("click", () => setMobileDrawerState(false));

  if (mobileAccordionBtn && mobileAccordion) {
    mobileAccordionBtn.addEventListener("click", () => {
      const isOpen = mobileAccordion.classList.toggle("is-open");
      if (mobileExpandIcon) {
        mobileExpandIcon.style.transform = isOpen
          ? "rotate(180deg)"
          : "rotate(0deg)";
      }
      mobileAccordionBtn.classList.toggle("text-primary", isOpen);
    });
  }

  /**
   * Utilities
   */
  document.addEventListener("keydown", (e) => {
    if (e.key === "Escape") {
      setMegaMenuState(false);
      setMobileDrawerState(false);
    }
  });

  window.addEventListener("resize", () => {
    if (window.innerWidth >= 1024) {
      setMobileDrawerState(false);
    } else {
      setMegaMenuState(false);
    }
  });
});

// FAQ
function initDGFaqSystem() {
  const faqItems = document.querySelectorAll(".dg-faq-item");
  const heroImage = document.querySelector(".dg-faq-hero-img");

  // Sequential reveal for FAQ items
  faqItems.forEach((item, index) => {
    item.style.opacity = "0";
    item.style.transform = "translateY(20px)";
    setTimeout(
      () => {
        item.style.transition = "all 0.6s cubic-bezier(0.23, 1, 0.32, 1)";
        item.style.opacity = "1";
        item.style.transform = "translateY(0)";
      },
      200 + index * 100,
    );

    // Interaction Logic
    const trigger = item.querySelector(".dg-faq-trigger");
    const icon = item.querySelector(".dg-faq-icon");

    if (trigger) {
      trigger.addEventListener("click", () => {
        const isActive = item.classList.contains("dg-faq-item-active");

        // Optional: Close others
        // faqItems.forEach(i => i.classList.remove('dg-faq-item-active'));

        if (isActive) {
          item.classList.remove("dg-faq-item-active");
          if (icon) icon.classList.remove("rotate-45");
        } else {
          item.classList.add("dg-faq-item-active");
          if (icon) icon.classList.add("rotate-45");
        }
      });
    }
  });

  // Parallax Interaction for cinematic hero
  if (heroImage) {
    window.addEventListener("mousemove", (e) => {
      const x = (window.innerWidth / 2 - e.pageX) / 45;
      const y = (window.innerHeight / 2 - e.pageY) / 45;
      heroImage.style.transform = `scale(1.1) translate(${x}px, ${y}px)`;
    });
  }
}

// Boot system on DOM load
document.addEventListener("DOMContentLoaded", initDGFaqSystem);


