import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    ClientSchema,
    CreateClientSchema,
    UpdateClientSchema,
} from "@/lib/client";
import {
    createClientMutation,
    deleteClientMutation,
    updateClientMutation,
} from "@/lib/client/@tanstack/react-query.gen";

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

export type ClientsCrud = {
    onCreate: (client: CreateClientSchema) => void;
    onUpdate: (clientId: number, client: UpdateClientSchema) => void;
    onDelete: (client: ClientSchema) => void;
};

export const useClientsCrud = ({ queryKey }: UseClientsProps): ClientsCrud => {
    return useCrudOperations<
        ClientSchema,
        UpdateClientSchema,
        CreateClientSchema
    >({
        key: "getClients",
        queryKey,
        mutations: {
            create: createClientMutation(),
            update: updateClientMutation(),
            delete: deleteClientMutation(),
        },
        getUpdateEntity: (old, update) => ({
            ...old,
            details: update.details ?? old.details,
            name: update.name ?? old.name,
            companyName: update.companyName ?? old.companyName,
            comment: update.comment ?? old.comment,
        }),
        getDeleteConfirmTitle: () => "Удаление клиента",
    });
};
