/* ==========================================
   DOCUMENT READY
========================================== */

document.addEventListener("DOMContentLoaded", function () {
  initAOS();
  captureUTMParameters();
  phoneValidation();
  smoothScroll();
  formValidation();
});

/* ==========================================
   AOS INIT
========================================== */

function initAOS() {
  AOS.init({
    duration: 1000,
    once: true,
    offset: 100,
  });
}

/* ==========================================
   UTM CAPTURE
========================================== */

function captureUTMParameters() {
  const params = new URLSearchParams(window.location.search);

  const utmSource = params.get("utm_source");
  const utmMedium = params.get("utm_medium");
  const utmCampaign = params.get("utm_campaign");

  const sourceField = document.getElementById("utm_source");
  const mediumField = document.getElementById("utm_medium");
  const campaignField = document.getElementById("utm_campaign");

  if (sourceField) {
    sourceField.value = utmSource || "";
  }

  if (mediumField) {
    mediumField.value = utmMedium || "";
  }

  if (campaignField) {
    campaignField.value = utmCampaign || "";
  }
}

/* ==========================================
   PHONE VALIDATION
========================================== */

function phoneValidation() {
  const phoneInput = document.querySelector('input[name="phone"]');

  if (!phoneInput) return;

  phoneInput.addEventListener("input", function () {
    this.value = this.value.replace(/\D/g, "");

    if (this.value.length > 10) {
      this.value = this.value.slice(0, 10);
    }
  });
}

/* ==========================================
   FORM VALIDATION
========================================== */

function formValidation() {
  const form = document.querySelector("form");

  if (!form) return;

  form.addEventListener("submit", function (e) {
    const name = form.querySelector('[name="name"]').value.trim();
    const email = form.querySelector('[name="email"]').value.trim();
    const phone = form.querySelector('[name="phone"]').value.trim();

    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (name.length < 3) {
      e.preventDefault();
      alert("Please enter a valid name.");

      return false;
    }

    if (!emailRegex.test(email)) {
      e.preventDefault();
      alert("Please enter a valid email address.");

      return false;
    }

    if (phone.length !== 10) {
      e.preventDefault();
      alert("Please enter a valid 10 digit mobile number.");

      return false;
    }

    const recaptcha = document.querySelector(".g-recaptcha-response");

    if (recaptcha && recaptcha.value === "") {
      e.preventDefault();
      alert("Please complete the reCAPTCHA.");

      return false;
    }
  });
}

/* ==========================================
   SMOOTH SCROLL
========================================== */

function smoothScroll() {
  const links = document.querySelectorAll('a[href^="#"]');

  links.forEach((link) => {
    link.addEventListener("click", function (e) {
      const targetId = this.getAttribute("href");

      const target = document.querySelector(targetId);

      if (target) {
        e.preventDefault();

        window.scrollTo({
          top: target.offsetTop ,
          behavior: "smooth",
        });
      }
    });
  });
}

/* ==========================================
   SCROLL TO FORM BUTTONS
========================================== */

// document.querySelectorAll(".scroll-to-form").forEach((btn) => {
//   btn.addEventListener("click", function (e) {
//     e.preventDefault();

//     const formSection = document.getElementById("enquiry-form");

//     console.log(formSection.offsetTop);
//     console.log(window.scrollY);
//     if (formSection) {
//       window.scrollTo({
//         top: target.offsetTop - 80,
//         behavior: "smooth",
//       });
//     }
//   });
// });

/* ==========================================
   PREVENT DOUBLE SUBMIT
========================================== */

const enquiryForm = document.querySelector("form");

if (enquiryForm) {
  enquiryForm.addEventListener("submit", function () {
    const submitBtn = this.querySelector('button[type="submit"]');

    if (submitBtn) {
      submitBtn.disabled = true;

      submitBtn.innerHTML = "Submitting...";
    }
  });
}

/* ==========================================
   LAZY LOAD ANIMATIONS
========================================== */

window.addEventListener("load", function () {
  document.body.classList.add("loaded");
});

/* ==========================================
   OPTIONAL: TRACK REFERRER
========================================== */

(function () {
  const referrer = document.referrer;

  if (referrer) {
    console.log("Traffic Source:", referrer);
  }
})();
