import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    CreateServicesKitSchema,
    ServicesKitSchema,
    UpdateServicesKitSchema,
} from "@/lib/client";
import {
    createServicesKitMutation,
    deleteServicesKitMutation,
    updateServicesKitMutation,
} from "@/lib/client/@tanstack/react-query.gen";

type Props = {
    queryKey: any[];
};

export type ServicesKitsCrud = {
    onCreate: (data: Partial<CreateServicesKitSchema>) => void;
    onUpdate: (
        servicesKitId: number,
        servicesKit: UpdateServicesKitSchema
    ) => void;
    onDelete: (servicesKit: ServicesKitSchema, onSuccess?: () => void) => void;
};

export const useServicesKitsCrud = ({ queryKey }: Props): ServicesKitsCrud => {
    return useCrudOperations<
        ServicesKitSchema,
        UpdateServicesKitSchema,
        CreateServicesKitSchema
    >({
        key: "getServicesKits",
        queryKey,
        mutations: {
            create: createServicesKitMutation(),
            update: updateServicesKitMutation(),
            delete: deleteServicesKitMutation(),
        },
        getCreateEntity: data => ({
            name: data.name!,
            serviceType: data.serviceType!,
            services: data.services!,
        }),
        getUpdateEntity: (old, update) => ({
            ...old,
            name: update.name ?? old.name,
            serviceType: update.serviceType ?? old.serviceType,
        }),
        getDeleteConfirmTitle: () => "Удаление набора услуг",
    });
};
