"use client";

import { useTranslations } from "next-intl";
import { useIsRtl } from "@/lib/use-is-rtl";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";

/**
 * Shared confirmation dialog (used for destructive delete actions across screens).
 * Keeps the dialog open while `busy` so the row stays until the mutation resolves;
 * the caller closes it via onOpenChange(false) on success.
 */
export function ConfirmDialog({
  open,
  onOpenChange,
  title,
  description,
  confirmLabel,
  onConfirm,
  busy = false,
  destructive = true,
}: {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  title: string;
  description?: string;
  confirmLabel?: string;
  onConfirm: () => void;
  busy?: boolean;
  destructive?: boolean;
}) {
  const tCommon = useTranslations("common");
  const isRtl = useIsRtl();

  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent dir={isRtl ? "rtl" : "ltr"}>
        <AlertDialogHeader>
          <AlertDialogTitle>{title}</AlertDialogTitle>
          {description ? (
            <AlertDialogDescription>{description}</AlertDialogDescription>
          ) : null}
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{tCommon("cancel")}</AlertDialogCancel>
          <AlertDialogAction
            className={destructive ? "bg-red-600 text-white hover:bg-red-700" : ""}
            disabled={busy}
            onClick={(e) => {
              e.preventDefault(); // keep open until the mutation resolves
              onConfirm();
            }}
          >
            {busy ? tCommon("loading") : confirmLabel ?? tCommon("delete")}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
