import { Module, forwardRef } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { EventPublisherService } from './event-publisher.service';
import { EventConsumerController } from './event-consumer.controller';
import { EventAckPublisher } from './event-ack.publisher';
import { EventAckController } from './event-ack.controller';
import { OutboxPublisherService } from './outbox-publisher.service';
import { OutboxRepository } from '../persistence/repositories/outbox.repository';
import { PrismaService } from '../persistence/prisma/prisma.service';
import { ApplicationModule } from '../../application/application.module';

@Module({
  imports: [
    forwardRef(() => ApplicationModule),
    ClientsModule.registerAsync([
      {
        name: 'KAFKA_SERVICE',
        imports: [ConfigModule],
        useFactory: (configService: ConfigService) => ({
          transport: Transport.KAFKA,
          options: {
            client: {
              clientId: configService.get<string>('KAFKA_CLIENT_ID', 'reward-service'),
              brokers: configService.get<string>('KAFKA_BROKERS', 'localhost:9092').split(','),
            },
            consumer: {
              groupId: configService.get<string>('KAFKA_GROUP_ID', 'reward-service-group'),
            },
          },
        }),
        inject: [ConfigService],
      },
    ]),
  ],
  controllers: [EventConsumerController, EventAckController],
  providers: [PrismaService, OutboxRepository, EventPublisherService, EventAckPublisher, OutboxPublisherService],
  exports: [EventPublisherService, EventAckPublisher, OutboxPublisherService, ClientsModule],
})
export class KafkaModule {}
