/* Wrap testimonial slider in an IIFE to avoid global name collisions
   with other scripts (banner slider uses similar variable names). */
(function () {
  /* ========== TESTIMONIAL SLIDER FUNCTIONALITY ========== */
  const track = document.getElementById("testimonialTrack");
  const slides = document.querySelectorAll(".testimonial-slide");
  const prevBtn = document.getElementById("prevBtn");
  const nextBtn = document.getElementById("nextBtn");
  const dotsContainer = document.getElementById("dotsContainer");

  if (!track || slides.length === 0 || !dotsContainer) {
    // Required DOM isn't present on this page; nothing to do.
    return;
  }

  let currentIndex = 0;
  let autoSlideInterval;

  // Create dots dynamically
  slides.forEach((_, i) => {
    const dot = document.createElement("div");
    dot.classList.add("dot");
    if (i === 0) dot.classList.add("active");
    dot.setAttribute("data-index", i);
    dot.addEventListener("click", () => goToSlide(i));
    dotsContainer.appendChild(dot);
  });

  const dots = dotsContainer.querySelectorAll(".dot");

  function updateSlides() {
    slides.forEach((slide, i) => {
      slide.classList.toggle("active", i === currentIndex);
    });
    dots.forEach((dot, i) => {
      dot.classList.toggle("active", i === currentIndex);
    });

    if (prevBtn) prevBtn.disabled = currentIndex === 0;
    if (nextBtn) nextBtn.disabled = currentIndex === slides.length - 1;
  }

  function goToSlide(index) {
    currentIndex = index;
    track.style.transform = `translateX(-${index * 100}%)`;
    updateSlides();
  }

  if (prevBtn)
    prevBtn.addEventListener("click", () => {
      if (currentIndex > 0) {
        currentIndex--;
        goToSlide(currentIndex);
      }
    });

  if (nextBtn)
    nextBtn.addEventListener("click", () => {
      if (currentIndex < slides.length - 1) {
        currentIndex++;
        goToSlide(currentIndex);
      }
    });

  /* ========== AUTO SLIDE ========== */
  function startAutoSlide() {
    stopAutoSlide();
    autoSlideInterval = setInterval(() => {
      currentIndex = (currentIndex + 1) % slides.length;
      goToSlide(currentIndex);
    }, 5000);
  }

  function stopAutoSlide() {
    if (autoSlideInterval) clearInterval(autoSlideInterval);
  }

  track.addEventListener("mouseenter", stopAutoSlide);
  track.addEventListener("mouseleave", startAutoSlide);

  /* ========== READ MORE FUNCTIONALITY ========== */
  const readMoreButtons = document.querySelectorAll(".read-more-btn");
  readMoreButtons.forEach((btn) => {
    btn.addEventListener("click", () => {
      const text = btn.previousElementSibling;
      if (btn.textContent === "READ MORE") {
        text.style.maxHeight = "none";
        btn.textContent = "READ LESS";
      } else {
        text.style.maxHeight = "6.5em";
        btn.textContent = "READ MORE";
      }
    });
  });

  /* ========== INITIALIZATION ========== */
  goToSlide(0);
  startAutoSlide();

  /* ========== KEYBOARD NAVIGATION ========== */
  document.addEventListener("keydown", (e) => {
    if (e.key === "ArrowLeft" && prevBtn && !prevBtn.disabled) prevBtn.click();
    if (e.key === "ArrowRight" && nextBtn && !nextBtn.disabled) nextBtn.click();
  });

  /* ========== TOUCH SWIPE SUPPORT ========== */
  let startX = 0;
  track.addEventListener("touchstart", (e) => (startX = e.touches[0].clientX));
  track.addEventListener("touchend", (e) => {
    const endX = e.changedTouches[0].clientX;
    if (startX - endX > 50 && nextBtn && !nextBtn.disabled) nextBtn.click();
    if (endX - startX > 50 && prevBtn && !prevBtn.disabled) prevBtn.click();
  });
})();
