import roleManager from '../lib/roles-manager';
import { computeConstants } from '../lib/const-store';
import { initializeProducts } from './product-store';
import { initializeProductTagStore } from './product-tag-store';
import { initializeSlotStore } from './slot-store';
import { initializeBannerStore } from './banner-store';

/**
 * Initialize all application stores
 * This function handles initialization of:
 * - Role Manager (fetches and caches all roles)
 * - Const Store (syncs constants from DB to Redis)
 * - Product Store (caches all products in Redis)
 * - Product Tag Store (caches all product tags in Redis)
 * - Slot Store (caches all delivery slots with products in Redis)
 * - Banner Store (caches all banners in Redis)
 */
export const initializeAllStores = async (): Promise<void> => {
  try {
    console.log('Starting application stores initialization...');

    await Promise.all([
      roleManager.fetchRoles(),
      computeConstants(),
      initializeProducts(),
      initializeProductTagStore(),
      initializeSlotStore(),
      initializeBannerStore(),
    ]);

    console.log('All application stores initialized successfully');
  } catch (error) {
    console.error('Application stores initialization failed:', error);
    throw error;
  }
};