// sticky header
let lastScrollTop = 0;
const header = document.querySelector("header");

// Guard for existence: skip the scroll handler on pages with no <header>.
if (header) {
  window.addEventListener("scroll", () => {
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop;

    if (scrollTop > 50) {
      header.classList.add("scroll-state");
    } else {
      header.classList.remove("scroll-state");
      header.classList.remove("show-topbar");
    }

    if (scrollTop < lastScrollTop) {
      header.classList.add("sticky");

      if (scrollTop <= 100) {
        header.classList.add("show-topbar");
      } else {
        header.classList.remove("show-topbar");
      }
    } else {
      header.classList.remove("show-topbar");
    }

    lastScrollTop = scrollTop <= 0 ? 0 : scrollTop;
  });
}

// pricing
document.addEventListener("DOMContentLoaded", function () {
  const buttons = document.querySelectorAll(".toggle button");
  const prices = document.querySelectorAll(".price");
  const duration = document.querySelectorAll(".duration");

  // Guard for existence: skip if the pricing toggle isn't on this page.
  if (!buttons.length) return;

  buttons.forEach((btn) => {
    btn.addEventListener("click", () => {
      buttons.forEach((b) => {
        b.classList.remove("active");
        b.setAttribute("aria-pressed", "false");
      });
      btn.classList.add("active");
      btn.setAttribute("aria-pressed", "true");

      const type = btn.dataset.type;

      prices.forEach((price) => {
        const value = price.dataset[type];
        price.textContent = `${value}`;
      });

      // Label comes from the button's data-label (translated in PHP), not a
      // hardcoded string, so the duration text stays translation-ready.
      duration.forEach((d) => (d.textContent = btn.dataset.label));
    });
  });
});

// blog
document.addEventListener("DOMContentLoaded", function () {
  const blogsPerPage = 3;
  const blogContainer = document.getElementById("blogs");
  const pagination = document.getElementById("pagination");

  // Exit if blog container or pagination elements don't exist
  if (!blogContainer || !pagination) return;

  const blogItems = Array.from(blogContainer.querySelectorAll(".blog-item"));

  const totalPages = Math.ceil(blogItems.length / blogsPerPage);
  let currentPage = 1;

  function nutrawell_showBlogs(page) {
    blogItems.forEach((item, index) => {
      item.style.display =
        index >= (page - 1) * blogsPerPage && index < page * blogsPerPage
          ? "block"
          : "none";
    });
  }

  function nutrawell_createPagination() {
    pagination.innerHTML = "";

    // Prev
    const prevBtn = document.createElement("button");
    prevBtn.innerHTML = `<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M10.06 12L11 11.06L7.94667 8L11 4.94L10.06 4L6.06 8L10.06 12Z" fill="#4A4A4A"/>
</svg>`;
    prevBtn.disabled = currentPage === 1;
    prevBtn.onclick = () => nutrawell_changePage(currentPage - 1);
    pagination.appendChild(prevBtn);

    for (let i = 1; i <= totalPages; i++) {
      if (i === 1 || i === totalPages || Math.abs(i - currentPage) <= 1) {
        const btn = document.createElement("button");
        btn.textContent = i;
        if (i === currentPage) btn.classList.add("active");
        btn.onclick = () => nutrawell_changePage(i);
        pagination.appendChild(btn);
      } else if (i === currentPage - 2 || i === currentPage + 2) {
        const dots = document.createElement("button");
        dots.textContent = "...";
        dots.disabled = true;
        pagination.appendChild(dots);
      }
    }

    // Next
    const nextBtn = document.createElement("button");
    nextBtn.innerHTML = `<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.94 4L6 4.94L9.05333 8L6 11.06L6.94 12L10.94 8L6.94 4Z" fill="#4A4A4A"/>
</svg>`;
    nextBtn.disabled = currentPage === totalPages;
    nextBtn.onclick = () => nutrawell_changePage(currentPage + 1);
    pagination.appendChild(nextBtn);
  }

  function nutrawell_changePage(page) {
    if (page < 1 || page > totalPages) return;
    currentPage = page;
    nutrawell_showBlogs(currentPage);
    nutrawell_createPagination();
  }

  nutrawell_showBlogs(currentPage);
  nutrawell_createPagination();
});

// Swiper slider for testimonials
document.addEventListener("DOMContentLoaded", function () {
  const testimonialsSwiper = document.querySelector(".review-right.swiper");

  if (testimonialsSwiper && typeof Swiper !== "undefined") {
    new Swiper(".review-right.swiper", {
      slidesPerView: 1,
      spaceBetween: 20,
      loop: true,
      autoplay: {
        delay: 5000,
        disableOnInteraction: false,
      },
      pagination: {
        el: ".swiper-pagination",
        clickable: true,
      },
      breakpoints: {
        768: {
          slidesPerView: 1,
        },
      },
    });
  }
});
