/**
 * Event Consumer Starter Service
 *
 * Starts Kafka consumers after all handlers are registered.
 * This ensures handlers are registered before consuming starts.
 */

import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { EventConsumerService } from '../../infrastructure/messaging/kafka/event-consumer.service';

@Injectable()
export class EventConsumerStarterService implements OnApplicationBootstrap {
  private readonly logger = new Logger(EventConsumerStarterService.name);

  constructor(private readonly eventConsumer: EventConsumerService) {}

  onApplicationBootstrap() {
    // Don't await - let HTTP server start immediately while Kafka rebalances in background
    this.eventConsumer.startConsuming().catch((error) => {
      this.logger.error('Failed to start MPC event consumers', error);
    });
    this.logger.log('MPC event consumers starting in background');
  }
}
