import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RatesController } from '../controllers/rates.controller';
import { CacheModule } from '../../infrastructure/cache/cache.module';
import { CarrierModule } from '../../infrastructure/carriers/carrier.module';
import { CsvRateModule } from '../../infrastructure/carriers/csv-loader/csv-rate.module';

// Import domain services
import { RateSearchService } from '@domain/services/rate-search.service';

// Import domain ports
import { RATE_QUOTE_REPOSITORY } from '@domain/ports/out/rate-quote.repository';
import { PORT_REPOSITORY } from '@domain/ports/out/port.repository';
import { CARRIER_REPOSITORY } from '@domain/ports/out/carrier.repository';
import { CACHE_PORT } from '@domain/ports/out/cache.port';

// Import infrastructure implementations
import { TypeOrmRateQuoteRepository } from '../../infrastructure/persistence/typeorm/repositories/typeorm-rate-quote.repository';
import { TypeOrmPortRepository } from '../../infrastructure/persistence/typeorm/repositories/typeorm-port.repository';
import { TypeOrmCarrierRepository } from '../../infrastructure/persistence/typeorm/repositories/typeorm-carrier.repository';
import { RateQuoteOrmEntity } from '../../infrastructure/persistence/typeorm/entities/rate-quote.orm-entity';
import { PortOrmEntity } from '../../infrastructure/persistence/typeorm/entities/port.orm-entity';
import { CarrierOrmEntity } from '../../infrastructure/persistence/typeorm/entities/carrier.orm-entity';

@Module({
  imports: [
    CacheModule,
    CarrierModule,
    CsvRateModule, // Import CSV rate module for CSV search service
    TypeOrmModule.forFeature([RateQuoteOrmEntity, PortOrmEntity, CarrierOrmEntity]),
  ],
  controllers: [RatesController],
  providers: [
    {
      provide: RATE_QUOTE_REPOSITORY,
      useClass: TypeOrmRateQuoteRepository,
    },
    {
      provide: PORT_REPOSITORY,
      useClass: TypeOrmPortRepository,
    },
    {
      provide: CARRIER_REPOSITORY,
      useClass: TypeOrmCarrierRepository,
    },
    {
      provide: RateSearchService,
      useFactory: (
        connectors: any[],
        cache: any,
        rateQuoteRepo: any,
        portRepo: any,
        carrierRepo: any,
      ) => {
        return new RateSearchService(connectors, cache, rateQuoteRepo, portRepo, carrierRepo);
      },
      inject: ['CarrierConnectors', CACHE_PORT, RATE_QUOTE_REPOSITORY, PORT_REPOSITORY, CARRIER_REPOSITORY],
    },
  ],
  exports: [RATE_QUOTE_REPOSITORY, RateSearchService],
})
export class RatesModule {}
