// Hamburger menu toggle
const hamburger = document.getElementById("hamburger");
const nav = document.querySelector("nav");
hamburger.addEventListener("click", () => {
  nav.classList.toggle("open");
});
hamburger.addEventListener("keypress", (e) => {
  if (e.key === "Enter" || e.key === " ") nav.classList.toggle("open");
});

// =============================================================
// Scroll progress bar
// =============================================================
const abProg = document.getElementById("ab-progress");
const abOnScroll = () => {
  const h = document.documentElement;
  const scrolled = h.scrollTop / (h.scrollHeight - h.clientHeight);
  abProg.style.width = scrolled * 100 + "%";
};
document.addEventListener("scroll", abOnScroll, { passive: true });
abOnScroll();

// =============================================================
// Reveal on view (IntersectionObserver)
// =============================================================
const abRevealEls = document.querySelectorAll(".ab-reveal");
const abIO = new IntersectionObserver(
  (entries) => {
    entries.forEach((e) => {
      if (e.isIntersecting) {
        e.target.classList.add("ab-in");
        abIO.unobserve(e.target);
      }
    });
  },
  { threshold: 0.18 }
);
abRevealEls.forEach((el) => abIO.observe(el));

// =============================================================
// Parallax (simple translate on scroll)
// =============================================================
const abParallax = document.getElementById("ab-parallax-one");
const abParallaxScroll = () => {
  if (!abParallax) return;
  const rect = abParallax.getBoundingClientRect();
  const offset = rect.top * -0.08; // tune depth
  abParallax.style.transform = `translateY(${offset}px)`;
};
document.addEventListener("scroll", abParallaxScroll, { passive: true });
abParallaxScroll();

// =============================================================
// Slider (testimonials) with dot navigation
// =============================================================
const abSlides = document.getElementById("ab-slides");
const abNav = document.getElementById("ab-slider-nav");
let abIndex = 0;
let abTimer;

const abShowSlide = (index) => {
  if (!abSlides) return;
  const total = abSlides.children.length;
  abIndex = (index + total) % total;
  abSlides.style.transform = `translateX(-${abIndex * 100}%)`;

  // update dots
  [...abNav.children].forEach((dot, i) => {
    dot.classList.toggle("active", i === abIndex);
  });
};

const abNextSlide = () => {
  abShowSlide(abIndex + 1);
};

const abPrevSlide = () => {
  abShowSlide(abIndex - 1);
};

const abStartAuto = () => {
  abStopAuto();
  abTimer = setInterval(abNextSlide, 5000); // auto every 5s
};

const abStopAuto = () => {
  if (abTimer) clearInterval(abTimer);
};

// Build dots dynamically
if (abSlides && abNav) {
  [...abSlides.children].forEach((_, i) => {
    const dot = document.createElement("span");
    dot.className = "ab-dot";
    dot.addEventListener("click", () => {
      abShowSlide(i);
      abStartAuto();
    });
    abNav.appendChild(dot);
  });
  abShowSlide(0);
  abStartAuto();
}

// =============================================================
// Accordion (Investor Benefits)
// =============================================================
const abAccords = document.querySelectorAll(".ab-accordion");
abAccords.forEach((accord) => {
  accord.addEventListener("click", () => {
    accord.classList.toggle("open");
    const panel = accord.nextElementSibling;
    if (accord.classList.contains("open")) {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } else {
      panel.style.maxHeight = null;
    }
  });
});

// =============================================================
// Theme toggle (manual override for system theme)
// =============================================================
const abToggleTheme = document.getElementById("ab-theme-toggle");
if (abToggleTheme) {
  abToggleTheme.addEventListener("click", () => {
    document.body.classList.toggle("ab-dark");
    if (document.body.classList.contains("ab-dark")) {
      localStorage.setItem("ab-theme", "dark");
    } else {
      localStorage.setItem("ab-theme", "light");
    }
  });

  // init based on saved pref
  const savedTheme = localStorage.getItem("ab-theme");
  if (savedTheme === "dark") {
    document.body.classList.add("ab-dark");
  } else if (savedTheme === "light") {
    document.body.classList.remove("ab-dark");
  }
}

// =============================================================
// Smooth scroll for internal links
// =============================================================
const abLinks = document.querySelectorAll('a[href^="#"]');
abLinks.forEach((link) => {
  link.addEventListener("click", (e) => {
    e.preventDefault();
    const target = document.querySelector(link.getAttribute("href"));
    if (target) {
      window.scrollTo({
        top: target.offsetTop - 60,
        behavior: "smooth",
      });
    }
  });
});

// =============================================================
// Animation on scroll (fade-in)
// =============================================================
const abObserver = new IntersectionObserver(
  (entries) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        entry.target.classList.add("ab-visible");
      }
    });
  },
  { threshold: 0.15 }
);

document.querySelectorAll(".ab-reveal").forEach((el) => {
  abObserver.observe(el);
});

document.addEventListener("DOMContentLoaded", function () {
  // Investor Accordion
  document.querySelectorAll("#ab-accordion .ab-acc-head").forEach(function (head) {
    head.addEventListener("click", function () {
      const item = head.parentElement;
      const body = item.querySelector(".ab-acc-body");
      item.classList.toggle("open");
      if (item.classList.contains("open")) {
        body.style.maxHeight = body.scrollHeight + "px";
        body.style.opacity = "1";
      } else {
        body.style.maxHeight = "0";
        body.style.opacity = "0";
      }
    });
  });

  // FAQ Accordion
  document.querySelectorAll("#ab-accordion-faq .ab-acc-head").forEach(function (head) {
    head.addEventListener("click", function () {
      const item = head.parentElement;
      const body = item.querySelector(".ab-acc-body");
      item.classList.toggle("open");
      if (item.classList.contains("open")) {
        body.style.maxHeight = body.scrollHeight + "px";
        body.style.opacity = "1";
      } else {
        body.style.maxHeight = "0";
        body.style.opacity = "0";
      }
    });
  });

  // Optional: Initialize all bodies as collapsed
  document.querySelectorAll(".ab-acc-body").forEach(function (body) {
    body.style.maxHeight = "0";
    body.style.opacity = "0";
    body.style.transition = "max-height 0.3s cubic-bezier(.4,0,.2,1), opacity 0.3s";
    body.style.overflow = "hidden";
  });
});