import { Module, Global } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { PrismaService } from './persistence/prisma/prisma.service';
import { PlantingOrderRepositoryImpl } from './persistence/repositories/planting-order.repository.impl';
import { PlantingPositionRepositoryImpl } from './persistence/repositories/planting-position.repository.impl';
import { PoolInjectionBatchRepositoryImpl } from './persistence/repositories/pool-injection-batch.repository.impl';
import { ContractTemplateRepositoryImpl } from './persistence/repositories/contract-template.repository.impl';
import { ContractSigningTaskRepositoryImpl } from './persistence/repositories/contract-signing-task.repository.impl';
import { OutboxRepository } from './persistence/repositories/outbox.repository';
import { PaymentCompensationRepository } from './persistence/repositories/payment-compensation.repository';
import { UnitOfWork, UNIT_OF_WORK } from './persistence/unit-of-work';
import { WalletServiceClient } from './external/wallet-service.client';
import { ReferralServiceClient } from './external/referral-service.client';
import { IdentityServiceClient } from './external/identity-service.client';
import { TreePricingAdminClient } from './external/tree-pricing-admin.client';
import { KafkaModule } from './kafka/kafka.module';
import { OutboxPublisherService } from './kafka/outbox-publisher.service';
import { EventAckController } from './kafka/event-ack.controller';
import { ContractSigningEventConsumer } from './kafka/contract-signing-event.consumer';
import { KycVerifiedEventConsumer } from './kafka/kyc-verified-event.consumer';
// [纯新增] 转让事件处理器
import { TransferLockHandler } from './kafka/transfer-lock.handler';
import { TransferExecuteHandler } from './kafka/transfer-execute.handler';
import { TransferRollbackHandler } from './kafka/transfer-rollback.handler';
import { PdfGeneratorService } from './pdf/pdf-generator.service';
import { MinioStorageService } from './storage/minio-storage.service';
import { PLANTING_ORDER_REPOSITORY } from '../domain/repositories/planting-order.repository.interface';
import { PLANTING_POSITION_REPOSITORY } from '../domain/repositories/planting-position.repository.interface';
import { POOL_INJECTION_BATCH_REPOSITORY } from '../domain/repositories/pool-injection-batch.repository.interface';
import { CONTRACT_TEMPLATE_REPOSITORY } from '../domain/repositories/contract-template.repository.interface';
import { CONTRACT_SIGNING_TASK_REPOSITORY } from '../domain/repositories/contract-signing-task.repository.interface';
import { PaymentCompensationService } from '../application/services/payment-compensation.service';
import { ContractSigningService } from '../application/services/contract-signing.service';

@Global()
@Module({
  imports: [
    HttpModule.register({
      timeout: 5000,
      maxRedirects: 5,
    }),
    KafkaModule,
  ],
  controllers: [
    EventAckController,
    ContractSigningEventConsumer,
    KycVerifiedEventConsumer,
    // [纯新增] 转让事件处理器
    TransferLockHandler,
    TransferExecuteHandler,
    TransferRollbackHandler,
  ],
  providers: [
    PrismaService,
    {
      provide: PLANTING_ORDER_REPOSITORY,
      useClass: PlantingOrderRepositoryImpl,
    },
    {
      provide: PLANTING_POSITION_REPOSITORY,
      useClass: PlantingPositionRepositoryImpl,
    },
    {
      provide: POOL_INJECTION_BATCH_REPOSITORY,
      useClass: PoolInjectionBatchRepositoryImpl,
    },
    {
      provide: CONTRACT_TEMPLATE_REPOSITORY,
      useClass: ContractTemplateRepositoryImpl,
    },
    {
      provide: CONTRACT_SIGNING_TASK_REPOSITORY,
      useClass: ContractSigningTaskRepositoryImpl,
    },
    {
      provide: UNIT_OF_WORK,
      useClass: UnitOfWork,
    },
    OutboxRepository,
    PaymentCompensationRepository,
    OutboxPublisherService,
    PaymentCompensationService,
    ContractSigningService,
    PdfGeneratorService,
    MinioStorageService,
    WalletServiceClient,
    ReferralServiceClient,
    IdentityServiceClient,
    TreePricingAdminClient,
  ],
  exports: [
    PrismaService,
    PLANTING_ORDER_REPOSITORY,
    PLANTING_POSITION_REPOSITORY,
    POOL_INJECTION_BATCH_REPOSITORY,
    CONTRACT_TEMPLATE_REPOSITORY,
    CONTRACT_SIGNING_TASK_REPOSITORY,
    UNIT_OF_WORK,
    OutboxRepository,
    PaymentCompensationRepository,
    OutboxPublisherService,
    PaymentCompensationService,
    ContractSigningService,
    PdfGeneratorService,
    MinioStorageService,
    WalletServiceClient,
    ReferralServiceClient,
    IdentityServiceClient,
    TreePricingAdminClient,
  ],
})
export class InfrastructureModule {}
