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...');

    // Initialize role manager
    await roleManager.fetchRoles();
    console.log('Role manager initialized successfully');

    // Compute and store constants in Redis
    await computeConstants();
    console.log('Const store initialized successfully');

    // Initialize product store in Redis
    await initializeProducts();
    console.log('Product store initialized successfully');

    // Initialize product tag store in Redis
    await initializeProductTagStore();
    console.log('Product tag store initialized successfully');

    // Initialize slot store in Redis
    await initializeSlotStore();
    console.log('Slot store initialized successfully');

    // Initialize banner store in Redis
    await initializeBannerStore();
    console.log('Banner store initialized successfully');

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