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

import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.review.dto.CreateReviewRuleDto;
import com.lesingle.modules.biz.review.entity.BizContestReviewRule;
import com.lesingle.modules.biz.review.service.IContestReviewRuleService;
import com.lesingle.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.List;
import java.util.Map;

@Tag(name = "评审规则")
@RestController
@RequestMapping("/contests/review-rules")
@RequiredArgsConstructor
public class ContestReviewRuleController {

    private final IContestReviewRuleService contestReviewRuleService;

    @GetMapping
    @RequirePermission("contest:read")
    @Operation(summary = "查询评审规则列表")
    public Result<PageResult<Map<String, Object>>> findAll(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String ruleName) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(contestReviewRuleService.findAll(page, pageSize, ruleName, tenantId));
    }

    @GetMapping("/select")
    @RequirePermission("contest:read")
    @Operation(summary = "查询评审规则选项列表")
    public Result<List<Map<String, Object>>> findAllForSelect() {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(contestReviewRuleService.findAllForSelect(tenantId));
    }

    @PostMapping
    @RequirePermission("contest:update")
    @Operation(summary = "创建评审规则")
    public Result<BizContestReviewRule> createRule(@Valid @RequestBody CreateReviewRuleDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(contestReviewRuleService.createRule(dto, tenantId));
    }

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

    @PatchMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "更新评审规则")
    public Result<BizContestReviewRule> updateRule(@PathVariable Long id, @RequestBody CreateReviewRuleDto dto) {
        return Result.success(contestReviewRuleService.updateRule(id, dto));
    }

    @DeleteMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "删除评审规则")
    public Result<Void> removeRule(@PathVariable Long id) {
        contestReviewRuleService.removeRule(id);
        return Result.success();
    }
}
