/**
 * app.js — Material Design 3 Panel Interactions
 */

// =============================================================================
// Navigation Drawer Toggle
// =============================================================================

document.addEventListener("DOMContentLoaded", function () {
  const menuToggle = document.getElementById("menuToggle");
  const navDrawer = document.getElementById("navDrawer");

  if (menuToggle && navDrawer) {
    menuToggle.addEventListener("click", function () {
      navDrawer.classList.toggle("open");
    });

    // Close drawer when clicking outside on mobile
    document.addEventListener("click", function (e) {
      if (
        window.innerWidth <= 1024 &&
        navDrawer.classList.contains("open") &&
        !navDrawer.contains(e.target) &&
        !menuToggle.contains(e.target)
      ) {
        navDrawer.classList.remove("open");
      }
    });
  }
});

// =============================================================================
// Delete Dialog
// =============================================================================

function showDeleteDialog(deleteUrl) {
  const dialog = document.getElementById("confirmDeleteDialog");
  const scrim = document.getElementById("dialogScrim");
  const confirmBtn = document.getElementById("confirmDeleteBtn");
  const cancelBtn = document.getElementById("cancelDelete");

  if (!dialog || !scrim || !confirmBtn) return;

  confirmBtn.href = deleteUrl;
  dialog.setAttribute("open", "");
  scrim.classList.add("active");

  function closeDialog() {
    dialog.removeAttribute("open");
    scrim.classList.remove("active");
  }

  cancelBtn.onclick = closeDialog;
  scrim.onclick = closeDialog;

  // Close on Escape
  function handleEscape(e) {
    if (e.key === "Escape") {
      closeDialog();
      document.removeEventListener("keydown", handleEscape);
    }
  }
  document.addEventListener("keydown", handleEscape);
}

// =============================================================================
// Snackbar Notifications
// =============================================================================

function showSnackbar(message, type = "info", duration = 5000) {
  const container = document.getElementById("snackbarContainer");
  if (!container) return;

  const snackbar = document.createElement("div");
  snackbar.className = `md-snackbar md-snackbar--${type}`;
  snackbar.textContent = message;

  container.appendChild(snackbar);

  setTimeout(function () {
    snackbar.style.animation = "snackbarOut 0.3s forwards";
    setTimeout(function () {
      snackbar.remove();
    }, 300);
  }, duration);
}
