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

type ProductCollectionsQueryParams = {
    q?: string;
    page?: number;
    limit?: number;
};

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

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

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

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