// ============================================
// Admin Panel - Core JavaScript
// ============================================

(function () {
  'use strict';

  // ---- THEME ----
  const THEME_KEY = 'adminPanelTheme';

  function applyTheme(theme) {
    document.documentElement.setAttribute('data-theme', theme);
    localStorage.setItem(THEME_KEY, theme);
    const sunIcon = document.getElementById('sunIcon');
    const moonIcon = document.getElementById('moonIcon');
    const themeBtn = document.getElementById('themeToggle');
    if (sunIcon && moonIcon) {
      sunIcon.style.display = theme === 'light' ? 'block' : 'none';
      moonIcon.style.display = theme === 'dark' ? 'block' : 'none';
    }
    if (themeBtn && themeBtn.classList.contains('theme-btn')) {
      themeBtn.textContent = theme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
    }
  }

  window.toggleTheme = function () {
    const current = document.documentElement.getAttribute('data-theme') || 'dark';
    applyTheme(current === 'dark' ? 'light' : 'dark');
  };

  // Init theme on load
  const savedTheme = localStorage.getItem(THEME_KEY) || 'dark';
  applyTheme(savedTheme);

  // ---- SIDEBAR ----
  window.openSidebar = function () {
    const sb = document.getElementById('sidebar');
    const ov = document.getElementById('sidebarOverlay');
    if (sb) sb.classList.add('open');
    if (ov) ov.classList.add('open');
    document.body.style.overflow = 'hidden';
  };

  window.closeSidebar = function () {
    const sb = document.getElementById('sidebar');
    const ov = document.getElementById('sidebarOverlay');
    if (sb) sb.classList.remove('open');
    if (ov) ov.classList.remove('open');
    document.body.style.overflow = '';
  };

  // ---- MODAL ----
  window.openModal = function (id) {
    const modal = document.getElementById(id);
    if (modal) {
      modal.classList.add('open');
      document.body.style.overflow = 'hidden';
    }
  };

  window.closeModal = function (id) {
    const modal = document.getElementById(id);
    if (modal) {
      modal.classList.remove('open');
      document.body.style.overflow = '';
    }
  };

  // Close modals on Escape
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') {
      document.querySelectorAll('.modal.open').forEach(function (m) {
        m.classList.remove('open');
      });
      document.body.style.overflow = '';
    }
  });

  // ---- TOAST ----
  let toastTimer = null;

  window.showToast = function (message, isSuccess) {
    const toast = document.getElementById('toast');
    if (!toast) return;
    toast.textContent = message;
    toast.className = 'toast ' + (isSuccess ? 'success' : 'error');
    // Force reflow
    void toast.offsetWidth;
    toast.classList.add('show');
    clearTimeout(toastTimer);
    toastTimer = setTimeout(function () {
      toast.classList.remove('show');
    }, 3500);
  };

  // ---- AUTO FADE ALERTS ----
  setTimeout(function () {
    document.querySelectorAll('.alert').forEach(function (a) {
      a.style.transition = 'opacity 0.5s';
      a.style.opacity = '0';
      setTimeout(function () { a.remove(); }, 500);
    });
  }, 4000);

})();
