import { useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from '../../../lib/apiClient';
import { MutationConfig } from '../../../lib/reactQuery';
import { ProductCollection } from '../types/api';
import { getProductCollectionsQueryOptions } from './getProductCollections';
import { CreateProductCollectionFormValues } from '../types/form';

export const createProductCollection = ({ data }: { data: CreateProductCollectionFormValues }): Promise<ProductCollection> => {
    return api.post('/inventory/collections', data);
};

type UseCreateProductCollectionOptions = {
    mutationConfig?: MutationConfig<typeof createProductCollection>;
};

export const useCreateProductCollection = ({ mutationConfig }: UseCreateProductCollectionOptions = {}) => {
    const queryClient = useQueryClient();

    const { onSuccess, ...restConfig } = mutationConfig || {};

    return useMutation({
        mutationFn: createProductCollection,
        onSuccess: (...args) => {
            queryClient.invalidateQueries({
                queryKey: getProductCollectionsQueryOptions().queryKey,
            });
            onSuccess?.(...args);
        },
        ...restConfig,
    });
};
