import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { BullModule } from "@nestjs/bullmq";
import { ThrottlerModule } from "@nestjs/throttler";
import { HealthModule } from "./api/health/health.module";
import { AgentsModule } from "./api/agents/agents.module";
import { MissionControlModule } from "./api/mission-control/mission-control.module";
import { QueueApiModule } from "./api/queue/queue-api.module";
import { AgentProvidersModule } from "./api/agent-providers/agent-providers.module";
import { ProvidersModule } from "./api/providers/providers.module";
import { CoordinatorModule } from "./coordinator/coordinator.module";
import { BudgetModule } from "./budget/budget.module";
import { CIModule } from "./ci";
import { orchestratorConfig } from "./config/orchestrator.config";

/**
 * Rate limiting configuration:
 * - 'default': Standard API endpoints (100 requests per minute)
 * - 'strict': Spawn/kill endpoints (10 requests per minute) - prevents DoS
 * - 'status': Status/health endpoints (200 requests per minute) - higher for polling
 */
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [orchestratorConfig],
    }),
    BullModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        connection: {
          host: configService.get<string>("orchestrator.valkey.host", "localhost"),
          port: configService.get<number>("orchestrator.valkey.port", 6379),
          password: configService.get<string>("orchestrator.valkey.password"),
        },
      }),
    }),
    ThrottlerModule.forRoot([
      {
        name: "default",
        ttl: 60000, // 1 minute
        limit: 100, // 100 requests per minute
      },
      {
        name: "strict",
        ttl: 60000, // 1 minute
        limit: 10, // 10 requests per minute for spawn/kill
      },
      {
        name: "status",
        ttl: 60000, // 1 minute
        limit: 200, // 200 requests per minute for status endpoints
      },
    ]),
    HealthModule,
    AgentsModule,
    AgentProvidersModule,
    ProvidersModule,
    MissionControlModule,
    QueueApiModule,
    CoordinatorModule,
    BudgetModule,
    CIModule,
  ],
})
export class AppModule {}
