import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Put,
} from '@nestjs/common';
import { CreateTaskDto, UpdateTaskDto, UpdateTaskStatusDto } from './tasks.dto';
import { TasksService } from './tasks.service';

@Controller('/api/tasks')
export class TasksController {
  constructor(private readonly tasksService: TasksService) {}

  @Get()
  getTasks(
    @Query('keyword') keyword?: string,
    @Query('status') status?: 'enabled' | 'disabled',
    @Query('preferredExecutor') preferredExecutor?: 'browser' | 'agent-s',
  ) {
    return this.tasksService.getTasks({ keyword, status, preferredExecutor });
  }

  @Post()
  createTask(@Body() body: CreateTaskDto) {
    return this.tasksService.createTask(body);
  }

  @Put('/:id')
  updateTask(@Param('id') id: string, @Body() body: UpdateTaskDto) {
    return this.tasksService.updateTask(id, body);
  }

  @Patch('/:id/status')
  updateStatus(@Param('id') id: string, @Body() body: UpdateTaskStatusDto) {
    return this.tasksService.updateStatus(id, body.status);
  }

  @Delete('/:id')
  deleteTask(@Param('id') id: string) {
    return this.tasksService.deleteTask(id);
  }

  @Post('/actions/delete')
  deleteTasks(
    @Body()
    body: {
      taskIds?: string[];
      filters?: {
        keyword?: string;
        status?: 'enabled' | 'disabled';
        preferredExecutor?: 'browser' | 'agent-s';
      };
    },
  ) {
    return this.tasksService.deleteTasks(body ?? {});
  }
}
