import Swal from "sweetalert2";

/** Orion admin palette — matches sidebar (#161c24) and page cards. */
export const ORION_SWAL_THEME = {
  container: "orion-swal-container",
  popup: "orion-swal-popup",
  title: "orion-swal-title",
  htmlContainer: "orion-swal-text",
  actions: "orion-swal-actions",
  confirmButton: "orion-swal-btn orion-swal-btn--danger",
  cancelButton: "orion-swal-btn orion-swal-btn--secondary",
  loader: "orion-swal-loader",
  icon: "orion-swal-icon",
} as const;

const OrionSwal = Swal.mixin({
  buttonsStyling: false,
  reverseButtons: true,
  focusCancel: true,
  customClass: ORION_SWAL_THEME,
  backdrop: "rgba(15, 23, 42, 0.55)",
  heightAuto: false,
  allowOutsideClick: false,
  allowEscapeKey: false,
});

/** Confirmation modal before deleting a CMS page. */
export async function confirmDeletePage(): Promise<boolean> {
  const result = await OrionSwal.fire({
    icon: "warning",
    title: "Delete Page?",
    html: "This action cannot be undone. The page and all associated content will be permanently removed.",
    showCancelButton: true,
    confirmButtonText: "Delete Page",
    cancelButtonText: "Cancel",
  });

  return result.isConfirmed;
}

/** Block interaction while the delete API request runs. */
export function showDeletePageLoading(): void {
  OrionSwal.fire({
    title: "Deleting page…",
    html: "Please wait while the page is removed.",
    showConfirmButton: false,
    didOpen: () => {
      Swal.showLoading();
    },
  });
}

export async function showDeletePageSuccess(): Promise<void> {
  await OrionSwal.fire({
    icon: "success",
    title: "Page deleted successfully",
    showConfirmButton: false,
    timer: 2200,
    timerProgressBar: true,
  });
}

export async function showDeletePageError(message: string): Promise<void> {
  await OrionSwal.fire({
    icon: "error",
    title: "Delete failed",
    text: message,
    confirmButtonText: "OK",
    showCancelButton: false,
    customClass: {
      ...ORION_SWAL_THEME,
      confirmButton: "orion-swal-btn orion-swal-btn--secondary",
    },
  });
}

export function closeAdminSwal(): void {
  Swal.close();
}
