import './notif-job';
import { initializeAllStores } from '../stores/store-initializer';
import { startOrderHandler, publishOrder } from './post-order-handler';

/**
 * 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)
 * - Other services can be added here in the future
 */
export const initFunc = async (): Promise<void> => {
  try {
    console.log('Starting application initialization...');

    // Initialize all stores
    await initializeAllStores();

    // Notification queue and worker are initialized via import
    console.log('Notification queue and worker initialized');

    // Start post order handler (Redis Pub/Sub subscriber)
    await startOrderHandler();

    // Wait a moment for subscription to be ready, then publish demo order
    // setTimeout(async () => {
    //   console.log('Publishing demo order for testing...');
    //   await publishOrder({
    //     orders: [{
    //       deliveryTime: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // Tomorrow
    //       orderPlaceTime: new Date().toISOString(),
    //       totalAmount: 550,
    //       orderItems: [
    //         { productName: "Chicken Breast", quantity: 2 },
    //         { productName: "Mutton Curry Cut", quantity: 1 },
    //       ],
    //     }],
    //   });
    // }, 20000);

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

export default initFunc;
