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

import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.review.dto.CreatePresetCommentDto;
import com.lesingle.modules.biz.review.dto.PresetCommentVo;
import com.lesingle.modules.biz.review.entity.BizPresetComment;
import com.lesingle.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<PresetCommentVo>> findAll() {
        Long judgeId = SecurityUtil.getCurrentUserId();
        return Result.success(presetCommentService.findAll(judgeId));
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询预设评语详情")
    public Result<PresetCommentVo> 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("/{id}/use")
    @Operation(summary = "增加评语使用次数")
    public Result<Void> incrementUseCount(@PathVariable Long id) {
        Long judgeId = SecurityUtil.getCurrentUserId();
        presetCommentService.incrementUseCount(id, judgeId);
        return Result.success();
    }
}
