import { ProductService }    from './product.service';
import { CategoryService }   from './category.service';
import { cache }             from './cache.service';
import { toProduct }         from '../models/product.model';
import { buildCategoryTree } from '../models/category.model';

export const prefetchAll = async () => {
  try {
    console.log('[Prefetch] Chargement initial des données Dolibarr...');

    const rawProducts = await ProductService.getAllRaw();
    cache.set('products:all', rawProducts.map(toProduct), 600);

    const rawCategories = await CategoryService.getAll();
    cache.set('categories:tree', buildCategoryTree(rawCategories), 600);

    console.log(`[Prefetch] ${rawProducts.length} produits, ${rawCategories.length} catégories chargés`);
  } catch (err) {
    console.error('[Prefetch] Erreur lors du chargement initial:', err);
  }
};