import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  ParseIntPipe,
} from '@nestjs/common';
import { JudgesService } from './judges.service';
import { CreateJudgeDto } from './dto/create-judge.dto';
import { UpdateJudgeDto } from './dto/update-judge.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/judges')
@UseGuards(JwtAuthGuard)
export class JudgesController {
  constructor(private readonly judgesService: JudgesService) {}

  @Post()
  @RequirePermission('contest:update')
  create(@Body() createJudgeDto: CreateJudgeDto, @Request() req) {
    const creatorId = req.user?.id;
    return this.judgesService.create(createJudgeDto, creatorId);
  }

  @Get('contest/:contestId')
  @RequirePermission('contest:read')
  findAll(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.judgesService.findAll(contestId);
  }

  @Get(':id')
  @RequirePermission('contest:read')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.judgesService.findOne(id);
  }

  @Patch(':id')
  @RequirePermission('contest:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateJudgeDto: UpdateJudgeDto,
    @Request() req,
  ) {
    const modifierId = req.user?.id;
    return this.judgesService.update(id, updateJudgeDto, modifierId);
  }

  @Delete(':id')
  @RequirePermission('contest:update')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.judgesService.remove(id);
  }
}

