import { Module, Global } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { ClientsModule, Transport } from '@nestjs/microservices'
import { EventPublisherService } from './event-publisher.service'
import { EventAckPublisher } from './event-ack.publisher'
import { OutboxPublisherService } from './outbox-publisher.service'
import { OutboxRepository } from '../persistence/repositories/outbox.repository'
import { PrismaService } from '../persistence/prisma/prisma.service'

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