import { queryOptions, useQuery } from '@tanstack/react-query';
import { QueryConfig } from '../../../lib/reactQuery';
import { api } from '../../../lib/apiClient';
import { ProductCollectionResponse } from '../types/api';

type ProductCollectionsQueryParams = {
    search?: string | null;
    page?: number;
    limit?: number;
};

export const getProductCollections = ({ search, page, limit }: ProductCollectionsQueryParams = {}): Promise<ProductCollectionResponse> => {
    return api.get('/inventory/collections', {
        params: { search, page, limit },
    });
};

export const getProductCollectionsQueryOptions = ({ search, page, limit }: ProductCollectionsQueryParams = {}) => {
    return queryOptions({
        queryKey: ['productCollections', { search, page, limit }],
        queryFn: () => getProductCollections({ search, page, limit }),
    });
};

type UseProductCollectionsOptions = {
    queryConfig?: QueryConfig<typeof getProductCollectionsQueryOptions>;
} & ProductCollectionsQueryParams;

export const useProductCollections = ({ search, page, limit, queryConfig }: UseProductCollectionsOptions = {}) => {
    return useQuery({
        ...getProductCollectionsQueryOptions({ search, page, limit }),
        ...queryConfig,
    });
};
