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

@Controller('judges-management')
@UseGuards(JwtAuthGuard)
export class JudgesManagementController {
  constructor(private readonly judgesManagementService: JudgesManagementService) {}

  /**
   * 创建评委
   */
  @Post()
  @RequirePermission('judge:create')
  create(@Body() createJudgeDto: CreateJudgeDto, @Request() req) {
    const creatorId = req.user?.id;
    return this.judgesManagementService.create(createJudgeDto, creatorId);
  }

  /**
   * 查询评委列表
   */
  @Get()
  @RequirePermission('judge:read')
  findAll(@Query() queryDto: QueryJudgeDto) {
    return this.judgesManagementService.findAll(queryDto);
  }

  /**
   * 获取评委详情
   */
  @Get(':id')
  @RequirePermission('judge:read')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.judgesManagementService.findOne(id);
  }

  /**
   * 更新评委信息
   */
  @Patch(':id')
  @RequirePermission('judge:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateJudgeDto: UpdateJudgeDto,
    @Request() req,
  ) {
    const modifierId = req.user?.id;
    return this.judgesManagementService.update(id, updateJudgeDto, modifierId);
  }

  /**
   * 删除评委
   */
  @Delete(':id')
  @RequirePermission('judge:delete')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.judgesManagementService.remove(id);
  }

  /**
   * 冻结评委
   */
  @Patch(':id/freeze')
  @RequirePermission('judge:update')
  freeze(@Param('id', ParseIntPipe) id: number) {
    return this.judgesManagementService.toggleStatus(id, 'disabled');
  }

  /**
   * 解冻评委
   */
  @Patch(':id/unfreeze')
  @RequirePermission('judge:update')
  unfreeze(@Param('id', ParseIntPipe) id: number) {
    return this.judgesManagementService.toggleStatus(id, 'enabled');
  }

  /**
   * 批量删除评委
   */
  @Post('batch-delete')
  @RequirePermission('judge:delete')
  batchRemove(@Body() body: { ids: number[] }) {
    return this.judgesManagementService.batchRemove(body.ids);
  }
}
