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

import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.biz.review.dto.CreatePresetCommentDto;
import com.competition.modules.biz.review.dto.SyncPresetCommentsDto;
import com.competition.modules.biz.review.entity.BizPresetComment;
import com.competition.modules.biz.review.service.IPresetCommentService;
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.List;
import java.util.Map;

@Tag(name = "预设评语")
@RestController
@RequestMapping("/contests/preset-comments")
@RequiredArgsConstructor
public class PresetCommentController {

    private final IPresetCommentService presetCommentService;

    @PostMapping
    @Operation(summary = "创建预设评语")
    public Result<BizPresetComment> createComment(@Valid @RequestBody CreatePresetCommentDto dto) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.createComment(dto, judgeId));
    }

    @GetMapping
    @Operation(summary = "查询预设评语列表")
    public Result<List<Map<String, Object>>> findAll(@RequestParam(required = false) Long contestId) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.findAll(contestId, judgeId));
    }

    @GetMapping("/judge/contests")
    @Operation(summary = "获取评委参与的赛事列表")
    public Result<List<Map<String, Object>>> getJudgeContests() {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.getJudgeContests(judgeId));
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询预设评语详情")
    public Result<Map<String, Object>> findDetail(@PathVariable Long id) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.findDetail(id, judgeId));
    }

    @PatchMapping("/{id}")
    @Operation(summary = "更新预设评语")
    public Result<BizPresetComment> updateComment(@PathVariable Long id, @RequestBody CreatePresetCommentDto dto) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.updateComment(id, dto, judgeId));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除预设评语")
    public Result<Void> removeComment(@PathVariable Long id) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        presetCommentService.removeComment(id, judgeId);
        return Result.success();
    }

    @PostMapping("/batch-delete")
    @Operation(summary = "批量删除预设评语")
    public Result<Void> batchDelete(@RequestBody Map<String, List<Long>> body) {
        List<Long> ids = body.get("ids");
        Long judgeId = SecurityUtil.getCurrentUserId();
        presetCommentService.batchDelete(ids, judgeId);
        return Result.success();
    }

    @PostMapping("/sync")
    @Operation(summary = "同步评语到其他赛事")
    public Result<Map<String, Object>> syncComments(@Valid @RequestBody SyncPresetCommentsDto dto) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.syncComments(dto.getSourceContestId(), dto.getTargetContestIds(), judgeId));
    }

    @PostMapping("/{id}/use")
    @Operation(summary = "增加评语使用次数")
    public Result<Void> incrementUseCount(@PathVariable Long id) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        presetCommentService.incrementUseCount(id, judgeId);
        return Result.success();
    }
}
