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

import com.competition.common.result.Result;
import com.competition.modules.biz.review.dto.ContestJudgesForContestVo;
import com.competition.modules.biz.review.entity.BizContestJudge;
import com.competition.modules.biz.review.service.IContestJudgeService;
import com.competition.security.annotation.RequirePermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "赛事评委")
@RestController
@RequestMapping("/contests/judges")
@RequiredArgsConstructor
public class ContestJudgeController {

    private final IContestJudgeService contestJudgeService;

    @PostMapping
    @RequirePermission("contest:update")
    @Operation(summary = "添加赛事评委")
    public Result<BizContestJudge> createJudge(@RequestBody Map<String, Object> body) {
        Long contestId = Long.valueOf(body.get("contestId").toString());
        Long judgeId = Long.valueOf(body.get("judgeId").toString());
        String specialty = (String) body.get("specialty");
        BigDecimal weight = body.get("weight") != null ? new BigDecimal(body.get("weight").toString()) : null;
        String description = (String) body.get("description");
        return Result.success(contestJudgeService.createJudge(contestId, judgeId, specialty, weight, description));
    }

    @GetMapping("/contest/{contestId}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询赛事评委列表",
            description = "返回 assigned（显式关联）与 implicitPool（平台隐式池）。添加评委抽屉仅用 assigned 回显；作品分配可选池为 assigned ∪ implicitPool（前端合并）。")
    public Result<ContestJudgesForContestVo> findByContest(@PathVariable Long contestId) {
        return Result.success(contestJudgeService.findByContest(contestId));
    }

    @GetMapping("/{id}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询评委详情")
    public Result<Map<String, Object>> findDetail(@PathVariable Long id) {
        return Result.success(contestJudgeService.findDetail(id));
    }

    @PatchMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "更新评委信息")
    public Result<BizContestJudge> updateJudge(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        String specialty = (String) body.get("specialty");
        BigDecimal weight = body.get("weight") != null ? new BigDecimal(body.get("weight").toString()) : null;
        String description = (String) body.get("description");
        return Result.success(contestJudgeService.updateJudge(id, specialty, weight, description));
    }

    @DeleteMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "移除评委")
    public Result<Void> removeJudge(@PathVariable Long id) {
        contestJudgeService.removeJudge(id);
        return Result.success();
    }
}
