import { useCallback } from "react";
import { toast } from "sonner";
import EnhancedDynamicModal from "@/record-management/common/EnhancedDynamicModal";
import { useUserRecords } from "../hooks/useUserRecords";
import React from "react";
import { useTranslation } from "react-i18next";

interface DeleteRecordProps {
  recordId: string;
  onSuccess: () => void;
  onClose: () => void;
}

const DeleteRecord: React.FC<DeleteRecordProps> = ({
  recordId,
  onSuccess,
  onClose,
}) => {
  const { refetch, deleteRecordAsync } = useUserRecords();
  const { t } = useTranslation();
  const handleDelete = useCallback(async () => {
    if (!recordId) throw new Error("Invalid record ID");

    await deleteRecordAsync(recordId);
    toast.success(t("userRecord.Record is deleted successfully"));
    refetch();
    onSuccess();
  }, [recordId, refetch, onSuccess, deleteRecordAsync, t]);

  return (
    <EnhancedDynamicModal
      open={true}
      onClose={onClose}
      title={t("userRecord.Delete Record")}
      actionConfig={{
        type: "delete",
        onAction: handleDelete,
        confirmText: t("userRecord.Confirm Delete"),
        confirmVariant: "default",
      }}
    />
  );
};

export default DeleteRecord;
