package com.competition.modules.biz.review.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.modules.biz.review.dto.AutoSetAwardsDto;
import com.competition.modules.biz.review.dto.BatchSetAwardsDto;
import com.competition.modules.biz.review.dto.SetAwardDto;
import com.competition.modules.biz.review.service.IContestResultService;
import com.competition.security.annotation.RequirePermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "成果发布")
@RestController
@RequestMapping("/contests/results")
@RequiredArgsConstructor
public class ContestResultController {

    private final IContestResultService contestResultService;

    @PostMapping("/{contestId}/calculate-scores")
    @RequirePermission("contest:update")
    @Operation(summary = "计算所有最终得分")
    public Result<Map<String, Object>> calculateAllFinalScores(@PathVariable Long contestId) {
        return Result.success(contestResultService.calculateAllFinalScores(contestId));
    }

    @PostMapping("/{contestId}/calculate-rankings")
    @RequirePermission("contest:update")
    @Operation(summary = "计算排名")
    public Result<Map<String, Object>> calculateRankings(@PathVariable Long contestId) {
        return Result.success(contestResultService.calculateRankings(contestId));
    }

    @PatchMapping("/work/{workId}/award")
    @RequirePermission("contest:update")
    @Operation(summary = "设置奖项")
    public Result<Void> setAward(@PathVariable Long workId, @Valid @RequestBody SetAwardDto dto) {
        contestResultService.setAward(workId, dto);
        return Result.success();
    }

    @PostMapping("/{contestId}/batch-set-awards")
    @RequirePermission("contest:update")
    @Operation(summary = "批量设置奖项")
    public Result<Map<String, Object>> batchSetAwards(
            @PathVariable Long contestId,
            @Valid @RequestBody BatchSetAwardsDto dto) {
        return Result.success(contestResultService.batchSetAwards(contestId, dto));
    }

    @PostMapping("/{contestId}/auto-set-awards")
    @RequirePermission("contest:update")
    @Operation(summary = "自动设置奖项")
    public Result<Map<String, Object>> autoSetAwards(
            @PathVariable Long contestId,
            @Valid @RequestBody AutoSetAwardsDto dto) {
        return Result.success(contestResultService.autoSetAwards(contestId, dto));
    }

    @PostMapping("/{contestId}/publish")
    @RequirePermission("contest:update")
    @Operation(summary = "发布成果")
    public Result<Void> publishResults(@PathVariable Long contestId) {
        contestResultService.publishResults(contestId);
        return Result.success();
    }

    @PostMapping("/{contestId}/unpublish")
    @RequirePermission("contest:update")
    @Operation(summary = "撤回成果发布")
    public Result<Void> unpublishResults(@PathVariable Long contestId) {
        contestResultService.unpublishResults(contestId);
        return Result.success();
    }

    @GetMapping("/{contestId}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询成果列表")
    public Result<PageResult<Map<String, Object>>> getResults(
            @PathVariable Long contestId,
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String workNo,
            @RequestParam(required = false) String accountNo) {
        return Result.success(contestResultService.getResults(contestId, page, pageSize, workNo, accountNo));
    }

    @GetMapping("/{contestId}/summary")
    @RequirePermission("contest:read")
    @Operation(summary = "查询成果统计摘要")
    public Result<Map<String, Object>> getResultsSummary(@PathVariable Long contestId) {
        return Result.success(contestResultService.getResultsSummary(contestId));
    }
}
