"use client";

import {Trash2} from "lucide-react";
import {ButtonWithConfirm} from "@/components/common/button-with-confirm";
import {useMutation} from "@tanstack/react-query";
import {useRouter} from "next/navigation";
import {toast} from "sonner";
import {deleteAgentAction} from "@/features/agents/agent-delete.action";
import {useIsMobile} from "@/hooks/use-mobile";

export type ButtonDeleteAgentProps = {
    text?: string,
    agentId: string,
    organizationId?: string
    organizationIds?: string[]
};

export const ButtonDeleteAgent = (props: ButtonDeleteAgentProps) => {
    const router = useRouter();
    const isMobile = useIsMobile();

    const mutation = useMutation({
        mutationFn: () => deleteAgentAction({agentId: props.agentId, organizationId: props.organizationId, organizationIds: props.organizationIds}),
        onSuccess: async (result: any) => {
            if (result.data?.success) {
                toast.success(result.data.actionSuccess.message);
                router.push(props.organizationId ? "/dashboard/settings?tab=agents" :  "/dashboard/agents");
            } else {
                toast.error(result.data.actionError.message || "Unknown error occurred.");
            }
        },
    });

    return (
        <ButtonWithConfirm
            title={props.text ? props.text : ""}
            description="Are you sure you want to remove this agent? This action cannot be undone."
            button={{
                main: {
                    text: props.text ? !isMobile ? props.text : "" : "",
                    variant: "outline",
                    icon: <Trash2 color="red"/>,
                },
                confirm: {
                    className: "w-full",
                    text: "Delete",
                    icon: <Trash2/>,
                    variant: "destructive",
                    onClick: () => {
                        mutation.mutate();
                    },
                },
                cancel: {
                    className: "w-full",
                    text: "Cancel",
                    icon: <Trash2/>,
                    variant: "outline",
                },
            }}
            isPending={mutation.isPending}
        />
    );
};
