import { Module, Global } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { PrismaModule } from './persistence/prisma/prisma.module';
import { TradingAccountRepository } from './persistence/repositories/trading-account.repository';
import { OrderRepository } from './persistence/repositories/order.repository';
import { OutboxRepository } from './persistence/repositories/outbox.repository';
import { TradingConfigRepository } from './persistence/repositories/trading-config.repository';
import { BlackHoleRepository } from './persistence/repositories/black-hole.repository';
import { CirculationPoolRepository } from './persistence/repositories/circulation-pool.repository';
import { PriceSnapshotRepository } from './persistence/repositories/price-snapshot.repository';
import { ProcessedEventRepository } from './persistence/repositories/processed-event.repository';
import { C2cOrderRepository } from './persistence/repositories/c2c-order.repository';
import { RedisService } from './redis/redis.service';
import { KafkaProducerService } from './kafka/kafka-producer.service';
import { UserRegisteredConsumer } from './kafka/consumers/user-registered.consumer';
import { CdcConsumerService } from './kafka/cdc-consumer.service';
import { MarketMakerDepositConsumerService } from './kafka/market-maker-deposit-consumer.service';
import { BlockchainClient } from './blockchain/blockchain.client';
import { IdentityClient } from './identity/identity.client';

@Global()
@Module({
  imports: [
    PrismaModule,
    HttpModule,
    ClientsModule.registerAsync([
      {
        name: 'KAFKA_CLIENT',
        imports: [ConfigModule],
        useFactory: (configService: ConfigService) => ({
          transport: Transport.KAFKA,
          options: {
            client: {
              clientId: 'trading-service',
              brokers: configService
                .get<string>('KAFKA_BROKERS', 'localhost:9092')
                .split(','),
            },
            producer: { allowAutoTopicCreation: true },
          },
        }),
        inject: [ConfigService],
      },
    ]),
  ],
  controllers: [UserRegisteredConsumer],
  providers: [
    TradingAccountRepository,
    OrderRepository,
    OutboxRepository,
    TradingConfigRepository,
    BlackHoleRepository,
    CirculationPoolRepository,
    PriceSnapshotRepository,
    ProcessedEventRepository,
    C2cOrderRepository,
    KafkaProducerService,
    CdcConsumerService,
    MarketMakerDepositConsumerService,
    BlockchainClient,
    IdentityClient,
    {
      provide: 'REDIS_OPTIONS',
      useFactory: (configService: ConfigService) => ({
        host: configService.get<string>('REDIS_HOST', 'localhost'),
        port: configService.get<number>('REDIS_PORT', 6379),
        password: configService.get<string>('REDIS_PASSWORD'),
        db: configService.get<number>('REDIS_DB', 12),
      }),
      inject: [ConfigService],
    },
    RedisService,
  ],
  exports: [
    TradingAccountRepository,
    OrderRepository,
    OutboxRepository,
    TradingConfigRepository,
    BlackHoleRepository,
    CirculationPoolRepository,
    PriceSnapshotRepository,
    ProcessedEventRepository,
    C2cOrderRepository,
    KafkaProducerService,
    RedisService,
    BlockchainClient,
    IdentityClient,
    ClientsModule,
  ],
})
export class InfrastructureModule {}
