import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    CreateDealTagSchema,
    DealTagSchema,
    UpdateDealTagSchema,
} from "@/lib/client";
import {
    createDealTagMutation,
    deleteDealTagMutation,
    updateDealTagMutation,
} from "@/lib/client/@tanstack/react-query.gen";

type UseDealTagsOperationsProps = {
    queryKey: any[];
    projectId: number;
};

export type DealTagsCrud = {
    onCreate: (data: CreateDealTagSchema) => void;
    onUpdate: (dealTagId: number, dealTag: UpdateDealTagSchema) => void;
    onDelete: (dealTag: DealTagSchema) => void;
};

export const useDealTagsCrud = ({
    queryKey,
    projectId,
}: UseDealTagsOperationsProps): DealTagsCrud => {
    return useCrudOperations<
        DealTagSchema,
        UpdateDealTagSchema,
        CreateDealTagSchema
    >({
        key: "getDealTags",
        queryKey,
        mutations: {
            create: createDealTagMutation(),
            update: updateDealTagMutation(),
            delete: deleteDealTagMutation(),
        },
        getCreateEntity: data => ({
            tagColorId: data.tagColorId!,
            name: data.name!,
            projectId,
        }),
        getUpdateEntity: (old, update) => ({
            ...old,
            name: update.name ?? old.name,
            tagColor: update.tagColor ?? old.tagColor,
            tagColorId: update.tagColor?.id ?? old.tagColorId,
        }),
        getDeleteConfirmTitle: () => "Удаление доски",
    });
};
