import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  UseGuards,
  Request,
  ParseIntPipe,
} from '@nestjs/common';
import { ReviewsService } from './reviews.service';
import { AssignWorkDto } from './dto/assign-work.dto';
import { CreateScoreDto } from './dto/create-score.dto';
import { BatchAssignDto } from './dto/batch-assign.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/reviews')
@UseGuards(JwtAuthGuard)
export class ReviewsController {
  constructor(private readonly reviewsService: ReviewsService) {}

  @Post('assign')
  @RequirePermission('review:assign')
  assignWork(
    @Body() assignWorkDto: AssignWorkDto,
    @Query('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const creatorId = req.user?.userId;
    return this.reviewsService.assignWork(assignWorkDto, contestId, creatorId);
  }

  @Post('batch-assign')
  @RequirePermission('review:assign')
  batchAssignWorks(
    @Body() batchAssignDto: BatchAssignDto,
    @Query('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const creatorId = req.user?.userId;
    return this.reviewsService.batchAssignWorks(
      contestId,
      batchAssignDto.workIds,
      batchAssignDto.judgeIds,
      creatorId,
    );
  }

  @Post('auto-assign')
  @RequirePermission('review:assign')
  autoAssignWorks(
    @Query('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const creatorId = req.user?.userId;
    return this.reviewsService.autoAssignWorks(contestId, creatorId);
  }

  @Post('score')
  @RequirePermission('review:score')
  score(@Body() createScoreDto: CreateScoreDto, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new Error('无法确定租户信息');
    }
    const judgeId = req.user?.userId;
    if (!judgeId) {
      throw new Error('无法确定评委信息');
    }
    return this.reviewsService.score(createScoreDto, judgeId, tenantId);
  }

  @Patch('score/:id')
  @RequirePermission('review:score')
  updateScore(
    @Param('id', ParseIntPipe) scoreId: number,
    @Body() updateScoreDto: Partial<CreateScoreDto>,
    @Request() req,
  ) {
    const judgeId = req.user?.userId;
    return this.reviewsService.updateScore(scoreId, updateScoreDto, judgeId);
  }

  @Get('assigned')
  @RequirePermission('review:read')
  getAssignedWorks(
    @Query('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const judgeId = req.user?.userId;
    return this.reviewsService.getAssignedWorks(judgeId, contestId);
  }

  @Get('judge/contests')
  @RequirePermission('review:score')
  getJudgeContests(@Request() req) {
    const judgeId = req.user?.userId;
    console.log('getJudgeContests - judgeId:', judgeId, 'user:', req.user);
    if (!judgeId) {
      throw new Error('无法确定评委信息');
    }
    return this.reviewsService.getJudgeContests(judgeId);
  }

  @Get('judge/contests/:contestId/works')
  @RequirePermission('review:score')
  getJudgeContestWorks(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Query('page') page: string,
    @Query('pageSize') pageSize: string,
    @Query('workNo') workNo: string,
    @Query('accountNo') accountNo: string,
    @Query('reviewStatus') reviewStatus: string,
    @Request() req,
  ) {
    const judgeId = req.user?.userId;
    return this.reviewsService.getJudgeContestWorks(judgeId, contestId, {
      page: page ? parseInt(page, 10) : 1,
      pageSize: pageSize ? parseInt(pageSize, 10) : 10,
      workNo,
      accountNo,
      reviewStatus,
    });
  }

  @Get('progress/:contestId')
  @RequirePermission('review:read')
  getReviewProgress(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.reviewsService.getReviewProgress(contestId);
  }

  @Get('work-status/:contestId')
  @RequirePermission('review:read')
  getWorkStatusStats(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.reviewsService.getWorkStatusStats(contestId);
  }

  @Get('work/:workId/scores')
  @RequirePermission('review:read')
  getWorkScores(@Param('workId', ParseIntPipe) workId: number) {
    return this.reviewsService.getWorkScores(workId);
  }

  @Get('work/:workId/final-score')
  @RequirePermission('review:read')
  calculateFinalScore(@Param('workId', ParseIntPipe) workId: number) {
    return this.reviewsService.calculateFinalScore(workId);
  }
}

