import { Global, Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { JwtModule } from '@nestjs/jwt';
import { PrismaService } from './persistence/prisma/prisma.service';
import { RedisService, AddressCacheService } from './redis';
import { EventPublisherService, WithdrawalEventConsumerService, AdoptionInjectionConsumerService } from './kafka';
import { EvmProviderAdapter, AddressDerivationAdapter, MnemonicDerivationAdapter, RecoveryMnemonicAdapter, BlockScannerService } from './blockchain';
import { MpcSigningClient } from './mpc';
import { DomainModule } from '@/domain/domain.module';
import {
  DEPOSIT_TRANSACTION_REPOSITORY,
  MONITORED_ADDRESS_REPOSITORY,
  BLOCK_CHECKPOINT_REPOSITORY,
  TRANSACTION_REQUEST_REPOSITORY,
  OUTBOX_EVENT_REPOSITORY,
  MARKET_MAKER_DEPOSIT_REPOSITORY,
  MARKET_MAKER_CHECKPOINT_REPOSITORY,
  POOL_ACCOUNT_DEPOSIT_REPOSITORY,
  POOL_ACCOUNT_CHECKPOINT_REPOSITORY,
  ADOPTION_INJECTION_RECORD_REPOSITORY,
} from '@/domain/repositories';
import {
  DepositTransactionRepositoryImpl,
  MonitoredAddressRepositoryImpl,
  BlockCheckpointRepositoryImpl,
  TransactionRequestRepositoryImpl,
  OutboxEventRepositoryImpl,
  MarketMakerDepositRepositoryImpl,
  MarketMakerCheckpointRepositoryImpl,
  PoolAccountDepositRepositoryImpl,
  PoolAccountCheckpointRepositoryImpl,
  AdoptionInjectionRecordRepositoryImpl,
} from './persistence/repositories';

@Global()
@Module({
  imports: [DomainModule, HttpModule, JwtModule.register({})],
  providers: [
    // 核心服务
    PrismaService,
    RedisService,
    EventPublisherService,
    WithdrawalEventConsumerService,
    AdoptionInjectionConsumerService,
    MpcSigningClient,

    // 区块链适配器
    EvmProviderAdapter,
    AddressDerivationAdapter,
    MnemonicDerivationAdapter,
    RecoveryMnemonicAdapter,
    BlockScannerService,

    // 缓存服务
    AddressCacheService,

    // 仓储实现 (依赖倒置)
    {
      provide: DEPOSIT_TRANSACTION_REPOSITORY,
      useClass: DepositTransactionRepositoryImpl,
    },
    {
      provide: MONITORED_ADDRESS_REPOSITORY,
      useClass: MonitoredAddressRepositoryImpl,
    },
    {
      provide: BLOCK_CHECKPOINT_REPOSITORY,
      useClass: BlockCheckpointRepositoryImpl,
    },
    {
      provide: TRANSACTION_REQUEST_REPOSITORY,
      useClass: TransactionRequestRepositoryImpl,
    },
    {
      provide: OUTBOX_EVENT_REPOSITORY,
      useClass: OutboxEventRepositoryImpl,
    },
    {
      provide: MARKET_MAKER_DEPOSIT_REPOSITORY,
      useClass: MarketMakerDepositRepositoryImpl,
    },
    {
      provide: MARKET_MAKER_CHECKPOINT_REPOSITORY,
      useClass: MarketMakerCheckpointRepositoryImpl,
    },
    {
      provide: POOL_ACCOUNT_DEPOSIT_REPOSITORY,
      useClass: PoolAccountDepositRepositoryImpl,
    },
    {
      provide: POOL_ACCOUNT_CHECKPOINT_REPOSITORY,
      useClass: PoolAccountCheckpointRepositoryImpl,
    },
    {
      provide: ADOPTION_INJECTION_RECORD_REPOSITORY,
      useClass: AdoptionInjectionRecordRepositoryImpl,
    },
  ],
  exports: [
    PrismaService,
    RedisService,
    EventPublisherService,
    WithdrawalEventConsumerService,
    AdoptionInjectionConsumerService,
    MpcSigningClient,
    EvmProviderAdapter,
    AddressDerivationAdapter,
    MnemonicDerivationAdapter,
    RecoveryMnemonicAdapter,
    BlockScannerService,
    AddressCacheService,
    DEPOSIT_TRANSACTION_REPOSITORY,
    MONITORED_ADDRESS_REPOSITORY,
    BLOCK_CHECKPOINT_REPOSITORY,
    TRANSACTION_REQUEST_REPOSITORY,
    OUTBOX_EVENT_REPOSITORY,
    MARKET_MAKER_DEPOSIT_REPOSITORY,
    MARKET_MAKER_CHECKPOINT_REPOSITORY,
    POOL_ACCOUNT_DEPOSIT_REPOSITORY,
    POOL_ACCOUNT_CHECKPOINT_REPOSITORY,
    ADOPTION_INJECTION_RECORD_REPOSITORY,
  ],
})
export class InfrastructureModule {}
