import { dolibarrRequest } from './dolibarr.service';
import { cache }           from './cache.service';

export interface DolibarrCategory {
  id:        number;
  label:     string;
  description: string;
  fk_parent: number;
  color:     string;
}

export const CategoryService = {
  getAll: async (): Promise<DolibarrCategory[]> => {
    const cached = cache.get<DolibarrCategory[]>('categories:raw');
    if (cached) return cached;

    const data = await dolibarrRequest<DolibarrCategory[]>(
      '/categories?type=product&limit=200'
    );
    cache.set('categories:raw', data, 600);
    return data;
  },

  getProductsByCategory: (categoryId: number) =>
    dolibarrRequest<{ id: number }[]>(`/categories/${categoryId}/objects?type=product`),

  // getCategoriesForProduct: (productId: number) =>
  //   dolibarrRequest<DolibarrCategory[]>(`/products/${productId}/categories`),
};