// ---------------------------
// journer-slider

/* ========== JOURNEY SLIDER FUNCTIONALITY ========== */
const sliderWrapper = document.getElementById("sliderWrapper");
const prevBtnJourney = document.getElementById("prevBtnJourney");
const nextBtnJourney = document.getElementById("nextBtnJourney");
const slides = document.querySelectorAll(".slide");
const milestoneText = document.querySelector(".milestone-text p");

// Only run journey slider code if elements exist
if (sliderWrapper && prevBtnJourney && nextBtnJourney && slides.length > 0) {
  const milestones = [
    "AVENTURINE founded by Mr. P.N.C. Menon",
    "First residential project launched",
    "Expansion into commercial development",
    "AVENTURINE Training Academy established",
    "International presence expanded",
    "Green building certification milestone",
    "Award-winning sustainable projects",
    "Celebrating excellence and innovation",
  ];

  let currentIndex = 0;
  let slidesToShow = 3;

  function updateSlidesToShow() {
    if (window.innerWidth <= 768) slidesToShow = 1;
    else if (window.innerWidth <= 1024) slidesToShow = 2;
    else slidesToShow = 3;
    updateSlider();
  }

  function updateSlider() {
    const slideWidth = 100 / slidesToShow;
    sliderWrapper.style.transform = `translateX(-${
      currentIndex * slideWidth
    }%)`;

    prevBtnJourney.disabled = currentIndex === 0;
    nextBtnJourney.disabled = currentIndex >= slides.length - slidesToShow;

    prevBtnJourney.style.opacity = prevBtnJourney.disabled ? "0.3" : "1";
    nextBtnJourney.style.opacity = nextBtnJourney.disabled ? "0.3" : "1";

    if (milestoneText) {
      milestoneText.textContent = milestones[currentIndex] || "";
    }
  }

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

  nextBtnJourney.addEventListener("click", () => {
    if (currentIndex < slides.length - slidesToShow) {
      currentIndex++;
      updateSlider();
    }
  });

  window.addEventListener("resize", updateSlidesToShow);

  // Keyboard support
  document.addEventListener("keydown", (e) => {
    if (e.key === "ArrowLeft" && !prevBtnJourney.disabled)
      prevBtnJourney.click();
    if (e.key === "ArrowRight" && !nextBtnJourney.disabled)
      nextBtnJourney.click();
  });

  // Swipe support
  let startX = 0;
  sliderWrapper.addEventListener(
    "touchstart",
    (e) => (startX = e.touches[0].clientX)
  );
  sliderWrapper.addEventListener("touchend", (e) => {
    const endX = e.changedTouches[0].clientX;
    if (startX - endX > 50 && !nextBtnJourney.disabled) nextBtnJourney.click();
    if (endX - startX > 50 && !prevBtnJourney.disabled) prevBtnJourney.click();
  });

  updateSlidesToShow();
}
