(function () {
  var POPUP_KEY = 'flooritPromo2026';
  var DELAY_MS  = 5000;
  var EXPIRY_MS = 7 * 24 * 60 * 60 * 1000;

  function isStored(key) {
    try {
      var val = localStorage.getItem(key);
      return val && Date.now() < parseInt(val, 10);
    } catch (e) { return false; }
  }

  function store(key) {
    try { localStorage.setItem(key, String(Date.now() + EXPIRY_MS)); } catch (e) {}
  }

  /* --- Topbar (always visible, no dismiss) --------------- */
  function initTopbar() {
    var bar      = document.getElementById('promo-topbar');
    var offerBtn = document.getElementById('promo-topbar-btn');
    if (!bar) return;
    bar.classList.add('visible');
    document.body.classList.add('has-topbar');
    if (offerBtn) offerBtn.addEventListener('click', openPopup);
  }

  /* --- Popup --------------------------------------------- */
  function openPopup() {
    var overlay = document.getElementById('promo-overlay');
    if (!overlay) return;
    overlay.style.display = 'flex';
    requestAnimationFrame(function () {
      requestAnimationFrame(function () { overlay.classList.add('visible'); });
    });
  }

  function closePopup() {
    var overlay = document.getElementById('promo-overlay');
    if (overlay) {
      overlay.classList.remove('visible');
      setTimeout(function () { overlay.style.display = 'none'; }, 350);
    }
    store(POPUP_KEY);
  }

  function initPopup() {
    if (isStored(POPUP_KEY)) return;
    var closeBtn = document.getElementById('promo-close');
    var overlay  = document.getElementById('promo-overlay');
    var form     = document.getElementById('promo-form');
    var submit   = document.getElementById('promo-submit');
    var errEl    = document.getElementById('promo-error');
    var success  = document.getElementById('promo-success');
    if (!overlay || !form) return;

    if (closeBtn) closeBtn.addEventListener('click', closePopup);
    overlay.addEventListener('click', function (e) {
      if (e.target === overlay) closePopup();
    });

    form.addEventListener('submit', function (e) {
      e.preventDefault();
      errEl.style.display = 'none';
      submit.disabled = true;
      submit.textContent = 'Sending...';
      var data = new FormData(form);
      fetch('/promo/', { method: 'POST', body: data })
        .then(function (r) { return r.json(); })
        .then(function (res) {
          if (res.ok) {
            form.style.display = 'none';
            success.style.display = 'block';
            store(POPUP_KEY);
            hideTopbar();
          } else {
            errEl.textContent = res.error || 'Something went wrong.';
            errEl.style.display = 'block';
            submit.disabled = false;
            submit.textContent = 'Claim My Discount';
          }
        })
        .catch(function () {
          errEl.textContent = 'Network error. Please try again.';
          errEl.style.display = 'block';
          submit.disabled = false;
          submit.textContent = 'Claim My Discount';
        });
    });

    setTimeout(openPopup, DELAY_MS);
  }

  function init() {
    initTopbar();
    initPopup();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();
