import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { ResultsService } from './results.service';
import { SetAwardDto } from './dto/set-award.dto';
import { BatchSetAwardsDto } from './dto/batch-set-awards.dto';
import { AutoSetAwardsDto } from './dto/auto-set-awards.dto';
import { QueryResultsDto } from './dto/query-results.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/results')
@UseGuards(JwtAuthGuard)
export class ResultsController {
  constructor(private readonly resultsService: ResultsService) {}

  /**
   * 计算所有作品的最终得分
   */
  @Post(':contestId/calculate-scores')
  @RequirePermission('contest:update')
  calculateAllFinalScores(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.resultsService.calculateAllFinalScores(contestId);
  }

  /**
   * 计算排名
   */
  @Post(':contestId/calculate-rankings')
  @RequirePermission('contest:update')
  calculateRankings(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.resultsService.calculateRankings(contestId);
  }

  /**
   * 设置单个作品奖项
   */
  @Patch('work/:workId/award')
  @RequirePermission('contest:update')
  setAward(
    @Param('workId', ParseIntPipe) workId: number,
    @Body() setAwardDto: SetAwardDto,
  ) {
    return this.resultsService.setAward(workId, setAwardDto);
  }

  /**
   * 批量设置奖项
   */
  @Post(':contestId/batch-set-awards')
  @RequirePermission('contest:update')
  batchSetAwards(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Body() batchSetAwardsDto: BatchSetAwardsDto,
  ) {
    return this.resultsService.batchSetAwards(contestId, batchSetAwardsDto);
  }

  /**
   * 根据排名自动设置奖项
   */
  @Post(':contestId/auto-set-awards')
  @RequirePermission('contest:update')
  autoSetAwards(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Body() autoSetAwardsDto: AutoSetAwardsDto,
  ) {
    return this.resultsService.autoSetAwards(contestId, autoSetAwardsDto);
  }

  /**
   * 发布成果
   */
  @Post(':contestId/publish')
  @RequirePermission('contest:update')
  publishResults(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.resultsService.publishResults(contestId);
  }

  /**
   * 撤回发布
   */
  @Post(':contestId/unpublish')
  @RequirePermission('contest:update')
  unpublishResults(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.resultsService.unpublishResults(contestId);
  }

  /**
   * 获取活动结果列表（作品列表）
   */
  @Get(':contestId')
  @RequirePermission('contest:read')
  getResults(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Query() queryDto: QueryResultsDto,
  ) {
    return this.resultsService.getResults(contestId, queryDto);
  }

  /**
   * 获取活动结果统计摘要
   */
  @Get(':contestId/summary')
  @RequirePermission('contest:read')
  getResultsSummary(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.resultsService.getResultsSummary(contestId);
  }
}
