import {
  AlertDialog,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/shared/common/ui/alert-dialog";
import { Button } from "@/shared/common/ui/button";
import { Loader2 } from "lucide-react";
import { toast } from "sonner";
import { useUnit } from "@/user-management/hooks/useUnit";
import { useTranslation } from "react-i18next";

interface DeleteUnitDialogProps {
  unitName: string;
  unitId: string;
  isOpen: boolean;
  onClose: () => void;
}

export const DeleteUnitDialog: React.FC<DeleteUnitDialogProps> = ({
  unitName,
  unitId,
  isOpen,
  onClose,
}) => {
  const { removeUnit, isDeleting } = useUnit();
  const { t } = useTranslation();

  const handleDelete = async () => {
    try {
      await removeUnit(unitId);
      toast.success(t("archive.unitArchivedSuccess", "Unit archived successfully."));
      onClose();
    } catch {
      // Hook's onError already routed through handleError; nothing to do here.
    }
  };

  return (
    <AlertDialog open={isOpen} onOpenChange={onClose}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            {t("archive.archiveUnitConfirm", "Are you sure you want to archive this unit?")}
          </AlertDialogTitle>
          <AlertDialogDescription>
            {t("archive.archiveUnitDescription", "This action will archive the unit")} {" "}
            <strong>{unitName}</strong>. {t("archive.archiveUnitCanBeRestored", "It can be restored later from the Archived Units section.")}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel disabled={isDeleting}>{t("common.Cancel")}</AlertDialogCancel>
          <Button
            variant="destructive"
            onClick={handleDelete}
            disabled={isDeleting}
          >
            {isDeleting && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
            {t("archive.archiveUnit", "Archive Unit")}
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};
