import { Module, Global } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { ConfigModule } from '@nestjs/config';
import { PrismaService } from '../infrastructure/database/prisma.service';
import { RedisService } from '../infrastructure/cache/redis.service';
import { LeaderboardCacheService } from '../infrastructure/cache/leaderboard-cache.service';
import { KafkaService } from '../infrastructure/messaging/kafka.service';
import { EventPublisherService } from '../infrastructure/messaging/event-publisher.service';
import { EventConsumerService } from '../infrastructure/messaging/event-consumer.service';
import { ReferralServiceClient } from '../infrastructure/external/referral-service.client';
import { IdentityServiceClient } from '../infrastructure/external/identity-service.client';
import { LeaderboardRankingRepositoryImpl } from '../infrastructure/repositories/leaderboard-ranking.repository.impl';
import { LeaderboardConfigRepositoryImpl } from '../infrastructure/repositories/leaderboard-config.repository.impl';
import { VirtualAccountRepositoryImpl } from '../infrastructure/repositories/virtual-account.repository.impl';
import { LEADERBOARD_RANKING_REPOSITORY } from '../domain/repositories/leaderboard-ranking.repository.interface';
import { LEADERBOARD_CONFIG_REPOSITORY } from '../domain/repositories/leaderboard-config.repository.interface';
import { VIRTUAL_ACCOUNT_REPOSITORY } from '../domain/repositories/virtual-account.repository.interface';

@Global()
@Module({
  imports: [
    ConfigModule,
    HttpModule.register({
      timeout: 5000,
      maxRedirects: 5,
    }),
  ],
  providers: [
    PrismaService,
    RedisService,
    LeaderboardCacheService,
    KafkaService,
    EventPublisherService,
    EventConsumerService,
    ReferralServiceClient,
    IdentityServiceClient,
    {
      provide: LEADERBOARD_RANKING_REPOSITORY,
      useClass: LeaderboardRankingRepositoryImpl,
    },
    {
      provide: LEADERBOARD_CONFIG_REPOSITORY,
      useClass: LeaderboardConfigRepositoryImpl,
    },
    {
      provide: VIRTUAL_ACCOUNT_REPOSITORY,
      useClass: VirtualAccountRepositoryImpl,
    },
  ],
  exports: [
    PrismaService,
    RedisService,
    LeaderboardCacheService,
    KafkaService,
    EventPublisherService,
    ReferralServiceClient,
    IdentityServiceClient,
    LEADERBOARD_RANKING_REPOSITORY,
    LEADERBOARD_CONFIG_REPOSITORY,
    VIRTUAL_ACCOUNT_REPOSITORY,
  ],
})
export class InfrastructureModule {}
