import React, { useCallback } from "react";
import { toast } from "sonner";
import EnhancedDynamicModal from "@/record-management/common/EnhancedDynamicModal";

import { useTranslation } from "react-i18next";
import { useMyCollaborations } from "../hooks/useMyCollaborations";

interface RejectCollaborationRecordProps {
  collaborationId: string;
  onSuccess: () => void;
  onClose: () => void;
}

const RejectCollaborationRecord: React.FC<RejectCollaborationRecordProps> = ({
  collaborationId,
  onSuccess,
  onClose,
}) => {
  const { signCollaboration, refetch, signStatus } = useMyCollaborations();

  const { t } = useTranslation();

  const handleReject = useCallback(
    async (data: any) => {
      if (!collaborationId) {
        toast.error("Invalid record ID");
        return;
      }

      try {
        const remark = data.remarks;
        signCollaboration({
          payload: {
            collabId: collaborationId,
            signStatus: "rejected",
            comment: remark || "No Comment/አስተያየት የለም",
          },
        }).then(() => {
          toast.success(t("userRecord.Record rejected successfully!"));
          onSuccess();
        });
      } catch (error: any) {
        toast.error(error.message || t("userRecord.Failed to approve record."));
      }
    },
    [collaborationId, refetch, onSuccess]
  );
  return (
    <EnhancedDynamicModal
      open={true}
      onClose={onClose}
      title={t("userRecord.Reject Record")}
      formConfig={{
        fields: [
          {
            type: "textarea",
            name: "remarks",
            label: t("ApprovalPage.Remark"),
            placeholder: t("ApprovalPage.Remark"),
            required: false,
          },
        ],
      }}
      actionConfig={{
        type: "reject",
        onAction: handleReject,
        confirmText: t("userRecord.Confirm Reject"),
        confirmVariant: "destructive",
        isLoading: signStatus.isLoading,
      }}
    />
  );
};

export default RejectCollaborationRecord;
