import '@/src/lib/notif-job'
import { initializeAllStores } from '@/src/stores/store-initializer'
import { initializeUserNegativityStore } from '@/src/stores/user-negativity-store'
import { startOrderHandler, startCancellationHandler, publishOrder } from '@/src/lib/post-order-handler'
import { deleteOrders } from '@/src/lib/delete-orders'
import { createAllCacheFiles } from '@/src/lib/cloud_cache'

/**
 * Initialize all application services
 * This function handles initialization of:
 * - Role Manager (fetches and caches all roles)
 * - Const Store (syncs constants from DB to Redis)
 * - Post Order Handler (Redis Pub/Sub subscriber)
 * - Cancellation Handler (Redis Pub/Sub subscriber for order cancellations)
 * - User Negativity Store (caches user negativity scores in Redis)
 * - Other services can be added here in the future
 */
export const initFunc = async (): Promise<void> => {
  try {
    console.log('Starting application initialization...');

    await Promise.all([
      initializeAllStores(),
      initializeUserNegativityStore(),
      startOrderHandler(),
      startCancellationHandler(),
    ]);

    // Create all cache files after stores are initialized
    await createAllCacheFiles();
    console.log('Cache files created successfully');

    console.log('Application initialization completed successfully');
  } catch (error) {
    console.error('Application initialization failed:', error);
    throw error;
  }
};

export default initFunc;
