import { Global, Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { PrismaService } from './persistence/prisma/prisma.service';
import { RedisService, AddressCacheService } from './redis';
import { EventPublisherService, MpcEventConsumerService, WithdrawalEventConsumerService } 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,
} from '@/domain/repositories';
import {
  DepositTransactionRepositoryImpl,
  MonitoredAddressRepositoryImpl,
  BlockCheckpointRepositoryImpl,
  TransactionRequestRepositoryImpl,
  OutboxEventRepositoryImpl,
} from './persistence/repositories';

@Global()
@Module({
  imports: [DomainModule, HttpModule],
  providers: [
    // 核心服务
    PrismaService,
    RedisService,
    EventPublisherService,
    MpcEventConsumerService,
    WithdrawalEventConsumerService,
    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,
    },
  ],
  exports: [
    PrismaService,
    RedisService,
    EventPublisherService,
    MpcEventConsumerService,
    WithdrawalEventConsumerService,
    MpcSigningClient,
    EvmProviderAdapter,
    AddressDerivationAdapter,
    MnemonicDerivationAdapter,
    RecoveryMnemonicAdapter,
    BlockScannerService,
    AddressCacheService,
    DEPOSIT_TRANSACTION_REPOSITORY,
    MONITORED_ADDRESS_REPOSITORY,
    BLOCK_CHECKPOINT_REPOSITORY,
    TRANSACTION_REQUEST_REPOSITORY,
    OUTBOX_EVENT_REPOSITORY,
  ],
})
export class InfrastructureModule {}
