import { Controller, Get, Post, Patch, Delete, Body, Param, Logger } from "@nestjs/common";
import { QualityGateConfigService } from "./quality-gate-config.service";
import { CreateQualityGateDto, UpdateQualityGateDto } from "./dto";
import type { QualityGate } from "@prisma/client";

/**
 * Controller for managing quality gate configurations per workspace
 */
@Controller("workspaces/:workspaceId/quality-gates")
export class QualityGateConfigController {
  private readonly logger = new Logger(QualityGateConfigController.name);

  constructor(private readonly qualityGateConfigService: QualityGateConfigService) {}

  /**
   * Get all quality gates for a workspace
   */
  @Get()
  async findAll(@Param("workspaceId") workspaceId: string): Promise<QualityGate[]> {
    this.logger.debug(`GET /workspaces/${workspaceId}/quality-gates`);
    return this.qualityGateConfigService.findAll(workspaceId);
  }

  /**
   * Get a specific quality gate
   */
  @Get(":id")
  async findOne(
    @Param("workspaceId") workspaceId: string,
    @Param("id") id: string
  ): Promise<QualityGate> {
    this.logger.debug(`GET /workspaces/${workspaceId}/quality-gates/${id}`);
    return this.qualityGateConfigService.findOne(workspaceId, id);
  }

  /**
   * Create a new quality gate
   */
  @Post()
  async create(
    @Param("workspaceId") workspaceId: string,
    @Body() createDto: CreateQualityGateDto
  ): Promise<QualityGate> {
    this.logger.log(`POST /workspaces/${workspaceId}/quality-gates`);
    return this.qualityGateConfigService.create(workspaceId, createDto);
  }

  /**
   * Update a quality gate
   */
  @Patch(":id")
  async update(
    @Param("workspaceId") workspaceId: string,
    @Param("id") id: string,
    @Body() updateDto: UpdateQualityGateDto
  ): Promise<QualityGate> {
    this.logger.log(`PATCH /workspaces/${workspaceId}/quality-gates/${id}`);
    return this.qualityGateConfigService.update(workspaceId, id, updateDto);
  }

  /**
   * Delete a quality gate
   */
  @Delete(":id")
  async delete(@Param("workspaceId") workspaceId: string, @Param("id") id: string): Promise<void> {
    this.logger.log(`DELETE /workspaces/${workspaceId}/quality-gates/${id}`);
    return this.qualityGateConfigService.delete(workspaceId, id);
  }

  /**
   * Reorder quality gates
   */
  @Post("reorder")
  async reorder(
    @Param("workspaceId") workspaceId: string,
    @Body() body: { gateIds: string[] }
  ): Promise<QualityGate[]> {
    this.logger.log(`POST /workspaces/${workspaceId}/quality-gates/reorder`);
    return this.qualityGateConfigService.reorder(workspaceId, body.gateIds);
  }

  /**
   * Seed default quality gates for a workspace
   */
  @Post("seed-defaults")
  async seedDefaults(@Param("workspaceId") workspaceId: string): Promise<QualityGate[]> {
    this.logger.log(`POST /workspaces/${workspaceId}/quality-gates/seed-defaults`);
    return this.qualityGateConfigService.seedDefaults(workspaceId);
  }
}
