import { Module, forwardRef } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ExchangeModule, ExchangeOptions } from '@edr/api-common';

import { BillingModule } from '../billing/billing.module';
import { CompaniesModule } from '../companies/companies.module';
import { FilesModule } from '../files/files.module';
import { MinioModule } from '../minio/minio.module';
import { RuleEngineModule } from '../rule-engine/rule-engine.module';
import { FileUploadSettingsModule } from '../file-upload-settings/file-upload-settings.module';
import { DropdownSettingsModule } from '../dropdown-settings/dropdown-settings.module';
import { SignaturesModule } from '../signatures/signatures.module';
import { OtpModule } from '../otp/otp.module';
import { NotificationsModule } from '../notifications/notifications.module';
import { NotificationInboxModule } from '../notification-inbox/notification-inbox.module';
import { BookingsModule } from '../bookings/bookings.module';
import { TrainSchedulingModule } from '../train-scheduling/train-scheduling.module';
import { ContractTemplatesModule } from '../contract-templates/contract-templates.module';

import { ContractsController } from './contracts.controller';
import { ContractsService } from './contracts.service';
import { ContractsRepository } from './contracts.repository';
import { ContractExpiryService } from './contract-expiry.service';
import { ContractPricingService } from './contract-pricing.service';
import { ContractNotifierService } from './contract-notifier.service';
import { ContractTransitionService } from './contract-transition.service';
import { ContractClearanceService } from './contract-clearance.service';
import { BookingClearanceService } from './booking-clearance.service';
import { ClearanceWorkflowService } from './clearance-workflow.service';
import { ContractBookingService } from './contract-booking.service';
import { ClearanceMilestoneService } from './clearance-milestone.service';
import { GlOperationsService } from './gl-operations.service';
import { BookingRequestService } from './booking-request.service';
import { BookingRequestRepository } from './booking-request.repository';

import { Contract } from './entities/contract.entity';
import { ContractRoute } from './entities/contract-route.entity';
import { ContractCargoScope } from './entities/contract-cargo-scope.entity';
import { ContractRateSnapshot } from './entities/contract-rate-snapshot.entity';
import { ContractSignature } from './entities/contract-signature.entity';
import { ContractApprovalStep } from './entities/contract-approval-step.entity';
import { ContractReviewNote } from './entities/contract-review-note.entity';
import { ContractDocumentRevision } from './entities/contract-document-revision.entity';
import { ContractDocumentHistoryService } from './contract-document-history.service';
import { ContractClearanceCycle } from './entities/contract-clearance-cycle.entity';
import { ContractDocumentReview } from './entities/contract-document-review.entity';
import { ClearanceMilestone } from './entities/clearance-milestone.entity';
import { ClearanceIncident } from './entities/clearance-incident.entity';
import { BookingRequest } from './entities/booking-request.entity';
import { Booking } from '../bookings/entities/booking.entity';
import { BookingContainerUnit } from '../bookings/entities/booking-container-unit.entity';

import { ContractPdfService } from '../../contracts/contract-pdf.service';
import { ContractRendererService } from '../../contracts/contract-renderer.service';
import { ContractTemplateResolver } from '../../contracts/contract-template.resolver';
import { ContractDocumentViewModelBuilder } from '../../contracts/contract-document-view-model.builder';

@Module({
  imports: [
    TypeOrmModule.forFeature([
      Contract,
      ContractRoute,
      ContractCargoScope,
      ContractRateSnapshot,
      ContractSignature,
      ContractApprovalStep,
      ContractReviewNote,
      ContractDocumentRevision,
      ContractClearanceCycle,
      ContractDocumentReview,
      ClearanceMilestone,
      ClearanceIncident,
      BookingRequest,
      Booking,
      BookingContainerUnit,
    ]),
    BillingModule,
    RuleEngineModule,
    FileUploadSettingsModule,
    DropdownSettingsModule,
    FilesModule,
    MinioModule,
    SignaturesModule,
    OtpModule,
    NotificationsModule,
    NotificationInboxModule,
    CompaniesModule,
    // Provides the admin-editable contract document templates consumed by
    // ContractDocumentViewModelBuilder when rendering contract PDFs.
    ContractTemplatesModule,
    // BookingsModule provides BookingsRepository/BookingPricingService used by the
    // contract PDF builders (they read a Booking today — see docs/new-doc.md §3.3).
    forwardRef(() => BookingsModule),
    // TrainSchedulingModule provides the config-driven booking-window gate used
    // by ContractBookingService.createUnderContract. forwardRef because
    // TrainSchedulingModule already imports ContractsModule.
    forwardRef(() => TrainSchedulingModule),
    ExchangeModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService): ExchangeOptions =>
        config.get<ExchangeOptions>('app.cbeExchange') ?? {},
    }),
  ],
  controllers: [ContractsController],
  providers: [
    ContractsService,
    ContractsRepository,
    ContractExpiryService,
    ContractPricingService,
    ContractNotifierService,
    ContractTransitionService,
    ContractDocumentHistoryService,
    ContractClearanceService,
    ClearanceWorkflowService,
    BookingClearanceService,
    ContractBookingService,
    ClearanceMilestoneService,
    GlOperationsService,
    BookingRequestService,
    BookingRequestRepository,
    // Contract PDF providers (template resolution + render + PDF) — stateless
    // helpers reused from src/contracts/.
    ContractTemplateResolver,
    ContractRendererService,
    ContractPdfService,
    ContractDocumentViewModelBuilder,
  ],
  exports: [
    ContractsService,
    ContractsRepository,
    ContractPricingService,
    ContractTransitionService,
    ContractClearanceService,
    ClearanceWorkflowService,
    BookingClearanceService,
    ContractBookingService,
    ClearanceMilestoneService,
  ],
})
export class ContractsModule {}
