import { Controller, Post, Patch, Get, Body, Param, UseGuards } from "@nestjs/common";
import { Throttle } from "@nestjs/throttler";
import { CoordinatorIntegrationService } from "./coordinator-integration.service";
import {
  CreateCoordinatorJobDto,
  UpdateJobStatusDto,
  UpdateJobProgressDto,
  CompleteJobDto,
  FailJobDto,
} from "./dto";
import type { CoordinatorJobResult, CoordinatorHealthStatus } from "./interfaces";
import { ApiKeyGuard } from "../common/guards";

/**
 * CoordinatorIntegrationController - REST API for Python coordinator communication
 *
 * SECURITY:
 * - All endpoints require API key authentication via X-API-Key header
 * - Rate limiting: 100 requests per minute per API key (default)
 * - Health endpoint: 300 requests per minute (higher for monitoring)
 *
 * Endpoints:
 * - POST /coordinator/jobs - Create a job from coordinator
 * - PATCH /coordinator/jobs/:id/status - Update job status
 * - PATCH /coordinator/jobs/:id/progress - Update job progress
 * - POST /coordinator/jobs/:id/complete - Mark job as complete
 * - POST /coordinator/jobs/:id/fail - Mark job as failed
 * - GET /coordinator/jobs/:id - Get job details
 * - GET /coordinator/health - Integration health check
 */
@Controller("coordinator")
@UseGuards(ApiKeyGuard)
@Throttle({ default: { ttl: 60000, limit: 100 } }) // 100 requests per minute
export class CoordinatorIntegrationController {
  constructor(private readonly service: CoordinatorIntegrationService) {}

  /**
   * Create a job from the coordinator
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Post("jobs")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async createJob(@Body() dto: CreateCoordinatorJobDto): Promise<CoordinatorJobResult> {
    return this.service.createJob(dto);
  }

  /**
   * Update job status from the coordinator
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Patch("jobs/:id/status")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async updateJobStatus(
    @Param("id") id: string,
    @Body() dto: UpdateJobStatusDto
  ): Promise<Awaited<ReturnType<typeof this.service.updateJobStatus>>> {
    return this.service.updateJobStatus(id, dto);
  }

  /**
   * Update job progress from the coordinator
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Patch("jobs/:id/progress")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async updateJobProgress(
    @Param("id") id: string,
    @Body() dto: UpdateJobProgressDto
  ): Promise<Awaited<ReturnType<typeof this.service.updateJobProgress>>> {
    return this.service.updateJobProgress(id, dto);
  }

  /**
   * Mark job as complete from the coordinator
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Post("jobs/:id/complete")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async completeJob(
    @Param("id") id: string,
    @Body() dto: CompleteJobDto
  ): Promise<Awaited<ReturnType<typeof this.service.completeJob>>> {
    return this.service.completeJob(id, dto);
  }

  /**
   * Mark job as failed from the coordinator
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Post("jobs/:id/fail")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async failJob(
    @Param("id") id: string,
    @Body() dto: FailJobDto
  ): Promise<Awaited<ReturnType<typeof this.service.failJob>>> {
    return this.service.failJob(id, dto);
  }

  /**
   * Get job details with events and steps
   *
   * Rate limit: 100 requests per minute per API key
   */
  @Get("jobs/:id")
  @Throttle({ default: { ttl: 60000, limit: 100 } })
  async getJobDetails(
    @Param("id") id: string
  ): Promise<Awaited<ReturnType<typeof this.service.getJobDetails>>> {
    return this.service.getJobDetails(id);
  }

  /**
   * Integration health check
   *
   * Rate limit: 300 requests per minute (higher for monitoring)
   */
  @Get("health")
  @Throttle({ default: { ttl: 60000, limit: 300 } })
  async getHealth(): Promise<CoordinatorHealthStatus> {
    return this.service.getIntegrationHealth();
  }
}
