import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
import { RunsService } from './runs.service';

@Controller('/api/runs')
export class RunsController {
  constructor(private readonly runsService: RunsService) {}

  @Get('/report')
  getInspectionReport(@Query('date') date?: string) {
    return this.runsService.getInspectionReport(date);
  }

  @Get()
  getRuns(
    @Query('taskId') taskId?: string,
    @Query('keyword') keyword?: string,
    @Query('status')
    status?: 'queued' | 'running' | 'success' | 'failed' | 'partial',
    @Query('executor') executor?: 'browser' | 'agent-s',
    @Query('errorTag') errorTag?: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    return this.runsService.getRuns({
      taskId,
      keyword,
      status,
      executor,
      errorTag,
      page: page ? Number(page) : undefined,
      pageSize: pageSize ? Number(pageSize) : undefined,
    });
  }

  @Get('/:runId')
  getRunDetail(@Param('runId') runId: string) {
    return this.runsService.getRunDetail(runId);
  }

  @Post('/execute/:taskId')
  executeTask(@Param('taskId') taskId: string) {
    return this.runsService.executeTask(taskId);
  }

  @Delete('/:runId')
  deleteRun(@Param('runId') runId: string) {
    return this.runsService.deleteRun(runId);
  }

  @Post('/actions/delete')
  deleteRuns(
    @Body()
    body: {
      runIds?: string[];
      filters?: {
        taskId?: string;
        keyword?: string;
        status?: 'queued' | 'running' | 'success' | 'failed' | 'partial';
        executor?: 'browser' | 'agent-s';
        errorTag?: string;
      };
    },
  ) {
    return this.runsService.deleteRuns(body ?? {});
  }
}
