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

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

export const getProductCategories = ({ q, page, limit }: ProductCategoriesQueryParams = {}): Promise<ProductCategoryResponse> => {
    return api.get('/inventory/categories', {
        params: { q, page, limit },
    });
};

export const getProductCategoriesQueryOptions = ({ q, page, limit }: ProductCategoriesQueryParams = {}) => {
    return queryOptions({
        queryKey: ['productCategories', { q, page, limit }],
        queryFn: () => getProductCategories({ q, page, limit }),
    });
};

type UseProductCategoriesOptions = {
    queryConfig?: QueryConfig<typeof getProductCategoriesQueryOptions>;
} & ProductCategoriesQueryParams;

export const useProductCategories = ({ q, page, limit, queryConfig }: UseProductCategoriesOptions = {}) => {
    return useQuery({
        ...getProductCategoriesQueryOptions({ q, page, limit }),
        ...queryConfig,
    });
};
