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

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

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

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

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

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