import { Module, Global } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { PrismaService } from './persistence/prisma/prisma.service';
import { TransferOrderRepositoryImpl } from './persistence/repositories/transfer-order.repository.impl';
import { OutboxRepository } from './persistence/repositories/outbox.repository';
import { UnitOfWork, UNIT_OF_WORK } from './persistence/unit-of-work';
import { WalletServiceClient } from './external/wallet-service.client';
import { PlantingServiceClient } from './external/planting-service.client';
import { IdentityServiceClient } from './external/identity-service.client';
import { KafkaModule } from './kafka/kafka.module';
import { OutboxPublisherService } from './kafka/outbox-publisher.service';
import { EventAckController } from './kafka/event-ack.controller';
import { SagaEventConsumer } from './kafka/saga-event.consumer';
import { TRANSFER_ORDER_REPOSITORY } from '../domain/repositories/transfer-order.repository.interface';

@Global()
@Module({
  imports: [
    HttpModule.register({
      timeout: 5000,
      maxRedirects: 5,
    }),
    KafkaModule,
  ],
  controllers: [EventAckController, SagaEventConsumer],
  providers: [
    PrismaService,
    {
      provide: TRANSFER_ORDER_REPOSITORY,
      useClass: TransferOrderRepositoryImpl,
    },
    {
      provide: UNIT_OF_WORK,
      useClass: UnitOfWork,
    },
    OutboxRepository,
    OutboxPublisherService,
    WalletServiceClient,
    PlantingServiceClient,
    IdentityServiceClient,
  ],
  exports: [
    PrismaService,
    TRANSFER_ORDER_REPOSITORY,
    UNIT_OF_WORK,
    OutboxRepository,
    OutboxPublisherService,
    WalletServiceClient,
    PlantingServiceClient,
    IdentityServiceClient,
  ],
})
export class InfrastructureModule {}
