import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ModelRouter } from './model-router';
import { PromptTemplateService } from './prompts/prompt-template.service';
import { AiCostCalculatorService } from './usage/ai-cost-calculator.service';
import { AiUsageLogService } from './usage/ai-usage-log.service';
import { AiGatewayService } from './gateway/ai-gateway.service';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { ActiveRecallAnalysisWorkflow } from './workflows/active-recall-analysis.workflow';
import { FeynmanEvaluationWorkflow } from './workflows/feynman-evaluation.workflow';
import { KnowledgeImportWorkflow } from './workflows/knowledge-import.workflow';
import { ReviewCardGenerationWorkflow } from './workflows/review-card-generation.workflow';
import { LearningTrendWorkflow } from './workflows/learning-trend.workflow';
import { AdminAiGatewayController } from './ai.controller';
import { MockAiProvider } from './providers/mock-ai.provider';
import { DeepSeekProvider } from './providers/deepseek.provider';
import { SiliconFlowProvider } from './providers/siliconflow.provider';
import { MiniMaxProvider } from './providers/minimax.provider';
import type { AiProvider } from './providers/ai-provider.interface';

@Module({
  imports: [ConfigModule],
  controllers: [AdminAiGatewayController],
  providers: [
    ModelRouter,
    PromptTemplateService,
    AiCostCalculatorService,
    AiUsageLogService,
    MockAiProvider,
    DeepSeekProvider,
    MiniMaxProvider,
    {
      provide: 'AI_PROVIDERS',
      useFactory: (
        mock: MockAiProvider,
        deepseek: DeepSeekProvider,
        minimax: MiniMaxProvider,
      ): Map<string, AiProvider> => {
        const map = new Map<string, AiProvider>();
        map.set(mock.name, mock);
        map.set(deepseek.name, deepseek);
        map.set(minimax.name, minimax);
        return map;
      },
      inject: [MockAiProvider, DeepSeekProvider, MiniMaxProvider],
    },
    {
      provide: AiGatewayService,
      useFactory: (
        modelRouter: ModelRouter,
        promptTemplate: PromptTemplateService,
        costCalculator: AiCostCalculatorService,
        usageLog: AiUsageLogService,
        prisma: PrismaService,
        providers: Map<string, AiProvider>,
      ) => {
        return new AiGatewayService(
          modelRouter,
          promptTemplate,
          costCalculator,
          usageLog,
          providers,
          prisma,
        );
      },
      inject: [
        ModelRouter,
        PromptTemplateService,
        AiCostCalculatorService,
        AiUsageLogService,
        PrismaService,
        'AI_PROVIDERS',
      ],
    },
    ActiveRecallAnalysisWorkflow,
    FeynmanEvaluationWorkflow,
    KnowledgeImportWorkflow,
    ReviewCardGenerationWorkflow,
    LearningTrendWorkflow,
  ],
  exports: [AiGatewayService, ActiveRecallAnalysisWorkflow, FeynmanEvaluationWorkflow, KnowledgeImportWorkflow, ReviewCardGenerationWorkflow, LearningTrendWorkflow],
})
export class AiModule {}
