import { Controller, Get, Post, Param, Query, UseGuards } from '@nestjs/common';
import { WorkflowExecutionsService } from './workflow-executions.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('integrations/workflows/:workflowId/executions')
@UseGuards(JwtAuthGuard)
export class WorkflowExecutionsController {
  constructor(private readonly execService: WorkflowExecutionsService) {}

  /**
   * GET /integrations/workflows/:workflowId/executions
   * Lista las ejecuciones de un workflow con paginación y filtro por status.
   */
  @Get()
  async listExecutions(
    @Workspace('id') wsId: string,
    @Param('workflowId') workflowId: string,
    @Query('status') status?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.execService.listExecutions(wsId, workflowId, {
      status,
      page: page ? parseInt(page, 10) : undefined,
      limit: limit ? parseInt(limit, 10) : undefined,
    });
  }

  /**
   * GET /integrations/workflows/:workflowId/executions/stats
   * Estadísticas de ejecución del workflow.
   */
  @Get('stats')
  async getStats(
    @Workspace('id') wsId: string,
    @Param('workflowId') workflowId: string,
  ) {
    return this.execService.getExecutionStats(wsId, workflowId);
  }

  /**
   * GET /integrations/workflows/:workflowId/executions/:executionId
   * Detalle de una ejecución específica.
   */
  @Get(':executionId')
  async getExecution(
    @Workspace('id') wsId: string,
    @Param('workflowId') _workflowId: string,
    @Param('executionId') executionId: string,
  ) {
    return this.execService.getExecution(wsId, executionId);
  }

  /**
   * POST /integrations/workflows/:workflowId/executions/:executionId/cancel
   * Cancela una ejecución en curso.
   */
  @Post(':executionId/cancel')
  async cancelExecution(
    @Workspace('id') wsId: string,
    @Param('workflowId') _workflowId: string,
    @Param('executionId') executionId: string,
  ) {
    return this.execService.cancelExecution(wsId, executionId);
  }

  /**
   * POST /integrations/workflows/:workflowId/executions/:executionId/retry
   * Reintenta una ejecución fallida.
   */
  @Post(':executionId/retry')
  async retryExecution(
    @Workspace('id') wsId: string,
    @Param('workflowId') _workflowId: string,
    @Param('executionId') executionId: string,
  ) {
    return this.execService.retryExecution(wsId, executionId);
  }
}