import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { PrismaService } from './persistence/prisma/prisma.service';
import { RewardLedgerEntryRepositoryImpl } from './persistence/repositories/reward-ledger-entry.repository.impl';
import { RewardSummaryRepositoryImpl } from './persistence/repositories/reward-summary.repository.impl';
import { SettlementRecordRepository } from './persistence/repositories/settlement-record.repository';
import { OutboxRepository } from './persistence/repositories/outbox.repository';
import { ReferralServiceClient } from './external/referral-service/referral-service.client';
import { AuthorizationServiceClient } from './external/authorization-service/authorization-service.client';
import { WalletServiceClient } from './external/wallet-service/wallet-service.client';
import { KafkaModule } from './kafka/kafka.module';
import { RedisModule } from './redis/redis.module';
import { REWARD_LEDGER_ENTRY_REPOSITORY } from '../domain/repositories/reward-ledger-entry.repository.interface';
import { REWARD_SUMMARY_REPOSITORY } from '../domain/repositories/reward-summary.repository.interface';
import { REFERRAL_SERVICE_CLIENT, AUTHORIZATION_SERVICE_CLIENT } from '../domain/services/reward-calculation.service';

@Module({
  imports: [ConfigModule, KafkaModule, RedisModule],
  providers: [
    PrismaService,
    OutboxRepository,
    SettlementRecordRepository,
    {
      provide: REWARD_LEDGER_ENTRY_REPOSITORY,
      useClass: RewardLedgerEntryRepositoryImpl,
    },
    {
      provide: REWARD_SUMMARY_REPOSITORY,
      useClass: RewardSummaryRepositoryImpl,
    },
    // Register concrete classes first
    ReferralServiceClient,
    AuthorizationServiceClient,
    WalletServiceClient,
    // Then provide token aliases using useExisting
    {
      provide: REFERRAL_SERVICE_CLIENT,
      useExisting: ReferralServiceClient,
    },
    {
      provide: AUTHORIZATION_SERVICE_CLIENT,
      useExisting: AuthorizationServiceClient,
    },
  ],
  exports: [
    PrismaService,
    OutboxRepository,
    SettlementRecordRepository,
    REWARD_LEDGER_ENTRY_REPOSITORY,
    REWARD_SUMMARY_REPOSITORY,
    REFERRAL_SERVICE_CLIENT,
    AUTHORIZATION_SERVICE_CLIENT,
    WalletServiceClient,
    KafkaModule,
    RedisModule,
  ],
})
export class InfrastructureModule {}
