import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { PresetCommentsService } from './preset-comments.service';
import { CreatePresetCommentDto } from './dto/create-preset-comment.dto';
import { UpdatePresetCommentDto } from './dto/update-preset-comment.dto';
import { SyncPresetCommentsDto } from './dto/sync-preset-comments.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';

@Controller('contests/preset-comments')
@UseGuards(JwtAuthGuard)
export class PresetCommentsController {
  constructor(private readonly presetCommentsService: PresetCommentsService) {}

  @Post()
  create(@Body() createDto: CreatePresetCommentDto, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.create(createDto, judgeId, judgeId);
  }

  @Get()
  findAll(
    @Query('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.findAll(contestId, judgeId);
  }

  @Get('judge/contests')
  getJudgeContests(@Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.getJudgeContests(judgeId);
  }

  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.findOne(id, judgeId);
  }

  @Patch(':id')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateDto: UpdatePresetCommentDto,
    @Request() req,
  ) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.update(id, updateDto, judgeId, judgeId);
  }

  @Delete(':id')
  remove(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.remove(id, judgeId);
  }

  @Post('batch-delete')
  batchDelete(@Body() body: { ids: number[] }, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.batchDelete(body.ids, judgeId);
  }

  @Post('sync')
  sync(@Body() syncDto: SyncPresetCommentsDto, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.sync(syncDto, judgeId, judgeId);
  }

  @Post(':id/use')
  incrementUseCount(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const judgeId = req.user?.userId;
    return this.presetCommentsService.incrementUseCount(id, judgeId);
  }
}
