import { BullModule } from '@nestjs/bullmq';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs';
import { DeleteReportHandler } from './application/commands/delete-report/delete-report.handler';
import { GenerateReportHandler, REPORT_GENERATION_QUEUE  } from './application/commands/generate-report/generate-report.handler';
import { GetReportHandler } from './application/queries/get-report/get-report.handler';
import { ListReportsHandler } from './application/queries/list-reports/list-reports.handler';
import { REPORT_REPOSITORY } from './domain/repositories/report.repository';
import { AI_NARRATIVE_SERVICE } from './domain/services/ai-narrative.service';
import { INFRASTRUCTURE_DATA_SERVICE } from './domain/services/infrastructure-data.service';
import { MACRO_DATA_SERVICE } from './domain/services/macro-data.service';
import { PDF_GENERATOR_SERVICE } from './domain/services/pdf-generator.service';
import { PDF_STORAGE_SERVICE } from './domain/services/pdf-storage.service';
import { PrismaReportRepository } from './infrastructure/repositories/prisma-report.repository';
import { ClaudeNarrativeService } from './infrastructure/services/claude-narrative.service';
import { PrismaInfrastructureDataService } from './infrastructure/services/infrastructure-data.service';
import { PrismaMacroDataService } from './infrastructure/services/macro-data.service';
import { PuppeteerPdfGeneratorService } from './infrastructure/services/pdf-generator.service';
import { MinioPdfStorageService } from './infrastructure/services/pdf-storage.service';
import { ReportGenerationProcessor } from './infrastructure/services/report-generation.processor';
import { ReportsController } from './presentation/controllers/reports.controller';

const CommandHandlers = [
  GenerateReportHandler,
  DeleteReportHandler,
];

const QueryHandlers = [
  GetReportHandler,
  ListReportsHandler,
];

@Module({
  imports: [
    CqrsModule,
    ConfigModule,
    BullModule.registerQueue({
      name: REPORT_GENERATION_QUEUE,
    }),
  ],
  controllers: [ReportsController],
  providers: [
    // Repositories
    { provide: REPORT_REPOSITORY, useClass: PrismaReportRepository },

    // Services
    { provide: MACRO_DATA_SERVICE, useClass: PrismaMacroDataService },
    { provide: INFRASTRUCTURE_DATA_SERVICE, useClass: PrismaInfrastructureDataService },
    { provide: AI_NARRATIVE_SERVICE, useClass: ClaudeNarrativeService },
    { provide: PDF_GENERATOR_SERVICE, useClass: PuppeteerPdfGeneratorService },
    { provide: PDF_STORAGE_SERVICE, useClass: MinioPdfStorageService },

    // BullMQ processor
    ReportGenerationProcessor,

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
  ],
  exports: [REPORT_REPOSITORY, MACRO_DATA_SERVICE, INFRASTRUCTURE_DATA_SERVICE],
})
export class ReportsModule {}
