import { Body, Controller, Get, Param, Patch, Query, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { LabOrgGuard } from '../../common/guards/lab-org.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ListLabTasksDto, LocateTaskPageDto, UpdateLabTaskDto } from './dto/tasks.dto';
import { TasksService } from './tasks.service';

@ApiTags('tasks')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, LabOrgGuard)
@Controller('tasks')
export class TasksController {
  constructor(private readonly tasksService: TasksService) {}

  @Get()
  @ApiOperation({ summary: 'List lab tasks' })
  list(@Query() query: ListLabTasksDto, @Req() req) {
    const organizationId = this.tasksService.getOrganizationIdFromUser(req.user);
    return this.tasksService.list(organizationId, req.user.id, query, req.user.language);
  }

  @Get('locate-page')
  @ApiOperation({ summary: 'Find pagination page for a task in the sorted list' })
  locatePage(@Query() query: LocateTaskPageDto, @Req() req) {
    const organizationId = this.tasksService.getOrganizationIdFromUser(req.user);
    return this.tasksService.locateTaskPage(organizationId, req.user.id, query);
  }

  @Get('filter-options')
  @ApiOperation({ summary: 'Filter options for lab tasks list' })
  listFilterOptions(@Req() req) {
    const organizationId = this.tasksService.getOrganizationIdFromUser(req.user);
    return this.tasksService.listFilterOptions(
      organizationId,
      req.user.id,
      req.user.language,
    );
  }

  @Patch(':taskId')
  @ApiOperation({ summary: 'Update task status' })
  updateStatus(
    @Param('taskId') taskId: string,
    @Body() dto: UpdateLabTaskDto,
    @Req() req,
  ) {
    const organizationId = this.tasksService.getOrganizationIdFromUser(req.user);
    return this.tasksService.updateStatus(
      taskId,
      dto,
      organizationId,
      req.user.id,
      req.user.language,
    );
  }
}
