import { useMutation } from '@tanstack/react-query';
import { useQueryClient } from '@tanstack/react-query';
import { api } from '../../../lib/apiClient';
import { MutationConfig } from '../../../lib/reactQuery';
import { getProductsQueryOptions } from './getProducts';

export const deleteProduct = ({ productId }: { productId: string | number }) => {
    return api.delete(`/inventory/products/${productId}`);
};

type UseDeleteProductOptions = {
    mutationConfig?: MutationConfig<typeof deleteProduct>;
};

export const useDeleteProduct = ({ mutationConfig }: UseDeleteProductOptions = {}) => {
    const queryClient = useQueryClient();

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

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