import { initializeWcagConfig, getWorkerConfig } from '@wcag-ada/config';
import { logger } from './utils/logger';
import { ScanWorker } from './workers/scan-worker';
import { SchedulerService } from './services/scheduler';
import { HealthService } from './services/health';
import { gracefulShutdown } from './utils/shutdown';

async function main() {
  try {
    // Initialize configuration
    const config = initializeWcagConfig('worker');
    const workerConfig = getWorkerConfig();
    
    logger.info('Starting WCAG-ADA Worker Service', {
      environment: config.environment,
      concurrency: workerConfig.concurrency,
      queue: workerConfig.queueName,
    });

    // Initialize services
    const scanWorker = new ScanWorker();
    const scheduler = new SchedulerService();
    const health = new HealthService();

    // Start services
    await scanWorker.start();
    logger.info('Scan worker started');

    if (workerConfig.scheduler.enabled) {
      await scheduler.start();
      logger.info('Scheduler service started');
    }

    await health.start();
    logger.info('Health service started');

    // Setup graceful shutdown
    gracefulShutdown([scanWorker, scheduler, health]);

    logger.info('WCAG-ADA Worker Service is running');
  } catch (error) {
    logger.error('Failed to start worker service', error);
    process.exit(1);
  }
}

main();