import { useMutation } from "@tanstack/react-query";
import { UpdateDealGroupSchema } from "@/lib/client";
import {
    addDealMutation,
    createDealGroupMutation,
    removeDealMutation,
    updateDealGroupMutation,
} from "@/lib/client/@tanstack/react-query.gen";

export type GroupsCrud = {
    onUpdate: (groupId: number, group: UpdateDealGroupSchema) => void;
    // onDelete: (group: DealGroupSchema, onSuccess?: () => void) => void;
};

const useDealGroupCrud = (): GroupsCrud => {
    const updateMutation = useMutation(updateDealGroupMutation());

    const onUpdate = (groupId: number, entity: UpdateDealGroupSchema) => {
        updateMutation.mutate({
            path: {
                pk: groupId,
            },
            body: {
                entity,
            },
        });
    };

    const createMutation = useMutation(createDealGroupMutation());

    const onCreate = (draggingDealId: number, hoveredDealId: number) => {
        createMutation.mutate({
            body: {
                draggingDealId,
                hoveredDealId,
            },
        });
    };

    const addDealToGroupMutation = useMutation(addDealMutation());

    const onAddDeal = (dealId: number, groupId: number) => {
        addDealToGroupMutation.mutate({
            body: {
                dealId,
                groupId,
            },
        });
    };

    const removeDealFromGroupMutation = useMutation(removeDealMutation());

    const onRemoveDeal = (dealId: number) => {
        removeDealFromGroupMutation.mutate({
            body: {
                dealId,
            },
        });
    };

    return {
        onUpdate,
        // onCreate,
        // onAddDeal,
        // onRemoveDeal,
    };
};

export default useDealGroupCrud;
