import {
  Controller,
  Post,
  Body,
  Param,
  UseGuards,
  Request,
  ParseIntPipe,
} from '@nestjs/common';
import { HomeworkScoresService } from './scores.service';
import { CreateHomeworkScoreDto } from './dto/create-score.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';
import { CurrentTenantId } from '../../auth/decorators/current-tenant-id.decorator';

@Controller('homework/scores')
@UseGuards(JwtAuthGuard)
export class HomeworkScoresController {
  constructor(private readonly scoresService: HomeworkScoresService) {}

  /**
   * 提交评分
   */
  @Post()
  @RequirePermission('homework:update')
  create(
    @Body() createDto: CreateHomeworkScoreDto,
    @CurrentTenantId() tenantId: number,
    @Request() req,
  ) {
    const reviewerId = req.user.id;
    return this.scoresService.create(createDto, tenantId, reviewerId);
  }

  /**
   * 标记作品违规
   */
  @Post(':submissionId/violation')
  @RequirePermission('homework:update')
  markViolation(
    @Param('submissionId', ParseIntPipe) submissionId: number,
    @Body('reason') reason: string,
    @CurrentTenantId() tenantId: number,
    @Request() req,
  ) {
    const reviewerId = req.user.id;
    return this.scoresService.markViolation(submissionId, tenantId, reviewerId, reason);
  }

  /**
   * 重置评分
   */
  @Post(':submissionId/reset')
  @RequirePermission('homework:update')
  resetScore(
    @Param('submissionId', ParseIntPipe) submissionId: number,
    @CurrentTenantId() tenantId: number,
    @Request() req,
  ) {
    const reviewerId = req.user.id;
    return this.scoresService.resetScore(submissionId, tenantId, reviewerId);
  }
}
