import * as React from "react";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Spinner } from "@/components/ui/spinner";

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export interface DeleteContactComponentProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  /** "single" — confirm deleting one contact by name. */
  mode: "single" | "bulk";
  /** Required when mode="single". */
  contactName?: string;
  /** Required when mode="bulk". Number of contacts to delete. */
  count?: number;
  onConfirm: () => void;
  isLoading?: boolean;
}

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

export function DeleteContactComponent({
  open,
  onOpenChange,
  mode,
  contactName,
  count = 0,
  onConfirm,
  isLoading = false,
}: DeleteContactComponentProps) {
  const isSingle = mode === "single";

  const title = isSingle
    ? `Delete ${contactName ?? "this client"}?`
    : `Delete ${count} client${count !== 1 ? "s" : ""}?`;

  const description = isSingle
    ? `This will permanently remove ${contactName ?? "this client"} and all their associated data. This action cannot be undone.`
    : `This will permanently remove ${count} selected client${count !== 1 ? "s" : ""} and all their associated data. This action cannot be undone.`;

  return (
    <AlertDialog
      open={open}
      onOpenChange={isLoading ? undefined : onOpenChange}
    >
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle className="text-destructive">
            {title}
          </AlertDialogTitle>
          <AlertDialogDescription>{description}</AlertDialogDescription>
        </AlertDialogHeader>

        <AlertDialogFooter>
          <AlertDialogCancel disabled={isLoading}>Cancel</AlertDialogCancel>
          <AlertDialogAction
            onClick={onConfirm}
            disabled={isLoading}
            className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
          >
            {isLoading ? (
              <>
                <Spinner className="size-3.5" />
                Deleting…
              </>
            ) : (
              "Delete"
            )}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
