package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.judge.CreateJudgeDTO;
import com.lesingle.creation.dto.judge.UpdateJudgeDTO;
import com.lesingle.creation.service.ContestJudgeService;
import com.lesingle.creation.vo.judge.JudgeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 竞赛评委管理控制器
 */
@Tag(name = "竞赛评委管理")
@RestController
@RequestMapping("/api/contests/judges")
@RequiredArgsConstructor
public class ContestJudgeController {

    private final ContestJudgeService judgeService;

    @PostMapping
    @Operation(summary = "创建评委")
    @PreAuthorize("hasAuthority('contest:judge:create')")
    public Result<JudgeVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateJudgeDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        JudgeVO result = judgeService.create(dto, creatorId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新评委")
    @PreAuthorize("hasAuthority('contest:judge:update')")
    public Result<JudgeVO> update(
            @PathVariable Long id,
            @RequestBody @Validated UpdateJudgeDTO dto) {
        JudgeVO result = judgeService.update(id, dto);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评委")
    @PreAuthorize("hasAuthority('contest:judge:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        judgeService.delete(id);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取评委详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<JudgeVO> getDetail(@PathVariable Long id) {
        JudgeVO result = judgeService.getDetail(id);
        return Result.success(result);
    }

    @GetMapping("/contest/{contestId}")
    @Operation(summary = "查询活动的评委列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<JudgeVO>> listByContest(@PathVariable Long contestId) {
        List<JudgeVO> result = judgeService.listByContest(contestId);
        return Result.success(result);
    }

    @PostMapping("/freeze")
    @Operation(summary = "冻结评委")
    @PreAuthorize("hasAuthority('contest:judge:update')")
    public Result<JudgeVO> freeze(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        JudgeVO result = judgeService.freeze(id);
        return Result.success(result);
    }

    @PostMapping("/unfreeze")
    @Operation(summary = "解冻评委")
    @PreAuthorize("hasAuthority('contest:judge:update')")
    public Result<JudgeVO> unfreeze(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        JudgeVO result = judgeService.unfreeze(id);
        return Result.success(result);
    }

    @PostMapping("/batch-delete")
    @Operation(summary = "批量删除评委")
    @PreAuthorize("hasAuthority('contest:judge:delete')")
    public Result<Void> batchDelete(@RequestBody List<Long> ids) {
        judgeService.batchDelete(ids);
        return Result.success(null);
    }
}
