import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ActivityEventConsumerController } from './activity-event-consumer.controller';
import { PrismaService } from '../persistence/prisma/prisma.service';
import { SystemActivityRepository } from '../persistence/repositories/system-activity.repository.impl';
import { RealtimeStatsRepository } from '../persistence/repositories/realtime-stats.repository.impl';
import { GlobalStatsRepository } from '../persistence/repositories/global-stats.repository.impl';
import {
  SYSTEM_ACTIVITY_REPOSITORY,
  REALTIME_STATS_REPOSITORY,
  GLOBAL_STATS_REPOSITORY,
} from '../../domain/repositories';

@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: 'KAFKA_SERVICE',
        imports: [ConfigModule],
        useFactory: (configService: ConfigService) => ({
          transport: Transport.KAFKA,
          options: {
            client: {
              clientId: configService.get<string>(
                'KAFKA_CLIENT_ID',
                'reporting-service',
              ),
              brokers: configService
                .get<string>('KAFKA_BROKERS', 'localhost:9092')
                .split(','),
            },
            consumer: {
              groupId: configService.get<string>(
                'KAFKA_GROUP_ID',
                'reporting-service-group',
              ),
            },
          },
        }),
        inject: [ConfigService],
      },
    ]),
  ],
  controllers: [ActivityEventConsumerController],
  providers: [
    PrismaService,
    {
      provide: SYSTEM_ACTIVITY_REPOSITORY,
      useClass: SystemActivityRepository,
    },
    {
      provide: REALTIME_STATS_REPOSITORY,
      useClass: RealtimeStatsRepository,
    },
    {
      provide: GLOBAL_STATS_REPOSITORY,
      useClass: GlobalStatsRepository,
    },
  ],
  exports: [ClientsModule],
})
export class KafkaModule {}
