// assets/verify-email.js

document.addEventListener('DOMContentLoaded', function () {
  const emailInput = document.querySelector('input[name="email"]');
  const form = document.getElementById('applicationForm');

  const sendCodeBtn = document.createElement('button');
  sendCodeBtn.type = 'button';
  sendCodeBtn.textContent = 'Send Verification Code';
  sendCodeBtn.style.marginLeft = '10px';

  const codeInput = document.createElement('input');
  codeInput.type = 'text';
  codeInput.name = 'email_code';
  codeInput.placeholder = 'Enter code';
  codeInput.style.display = 'none';
  codeInput.style.marginTop = '10px';

  const verifyBtn = document.createElement('button');
  verifyBtn.type = 'button';
  verifyBtn.textContent = 'Verify';
  verifyBtn.style.display = 'none';
  verifyBtn.style.marginLeft = '10px';

  const statusSpan = document.createElement('span');
  statusSpan.style.display = 'block';
  statusSpan.style.marginTop = '10px';

  // Inject UI
  emailInput.parentNode.appendChild(sendCodeBtn);
  emailInput.parentNode.appendChild(codeInput);
  emailInput.parentNode.appendChild(verifyBtn);
  emailInput.parentNode.appendChild(statusSpan);

  // Handle code request
  sendCodeBtn.addEventListener('click', () => {
    if (!emailInput.value) {
      statusSpan.style.color = 'red';
      statusSpan.textContent = 'Please enter your email address first.';
      return;
    }

    fetch('verify-email.php', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: 'email=' + encodeURIComponent(emailInput.value)
    })
      .then(res => res.json())
      .then(data => {
        if (data.success) {
          statusSpan.style.color = 'green';
          statusSpan.textContent = '📩 Code sent to your email.';
          codeInput.style.display = 'inline-block';
          verifyBtn.style.display = 'inline-block';
        } else {
          statusSpan.style.color = 'red';
          statusSpan.textContent = data.error;
        }
      });
  });

  // Handle verification
  verifyBtn.addEventListener('click', () => {
    fetch('verify-code.php', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: 'code=' + encodeURIComponent(codeInput.value)
    })
      .then(res => res.json())
      .then(data => {
        if (data.success) {
          statusSpan.style.color = 'green';
          statusSpan.innerHTML = '✅ Email verified successfully.';
          verifyBtn.disabled = true;
          sendCodeBtn.disabled = true;
          codeInput.readOnly = true;
          window.emailVerified = true;
        } else {
          statusSpan.style.color = 'red';
          statusSpan.textContent = '❌ Invalid code. Try again.';
        }
      });
  });

  // Prevent form submission if not verified
  form.addEventListener('submit', (e) => {
    if (!window.emailVerified) {
      e.preventDefault();
      statusSpan.style.color = 'red';
      statusSpan.textContent = '⚠️ Please verify your email before proceeding.';
      codeInput.focus();
    }
  });
});