package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.reviewrule.CreateReviewDimensionDTO;
import com.lesingle.creation.dto.reviewrule.CreateReviewRuleDTO;
import com.lesingle.creation.service.ContestReviewRuleService;
import com.lesingle.creation.vo.reviewrule.ReviewRuleVO;
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;

/**
 * 竞赛评审规则管理控制器
 */
@Tag(name = "竞赛评审规则管理")
@RestController
@RequestMapping("/api/contests/review-rules")
@RequiredArgsConstructor
public class ContestReviewRuleController {

    private final ContestReviewRuleService reviewRuleService;

    @PostMapping
    @Operation(summary = "创建评审规则")
    @PreAuthorize("hasAuthority('contest:review-rule:create')")
    public Result<ReviewRuleVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateReviewRuleDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        ReviewRuleVO result = reviewRuleService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评审规则")
    @PreAuthorize("hasAuthority('contest:review-rule:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        reviewRuleService.delete(id);
        return Result.success(null);
    }

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

    @GetMapping("/list")
    @Operation(summary = "查询评审规则列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<ReviewRuleVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<ReviewRuleVO> result = reviewRuleService.list(tenantId);
        return Result.success(result);
    }

    @GetMapping("/select")
    @Operation(summary = "获取可选的评审规则")
    public Result<List<ReviewRuleVO>> listForSelect(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<ReviewRuleVO> result = reviewRuleService.listForSelect(tenantId);
        return Result.success(result);
    }

    @PostMapping("/dimensions")
    @Operation(summary = "添加评审维度")
    @PreAuthorize("hasAuthority('contest:review-rule:update')")
    public Result<ReviewRuleVO> addDimension(@RequestBody @Validated CreateReviewDimensionDTO dto) {
        ReviewRuleVO result = reviewRuleService.addDimension(dto);
        return Result.success(result);
    }

    @DeleteMapping("/dimensions/{id}")
    @Operation(summary = "删除评审维度")
    @PreAuthorize("hasAuthority('contest:review-rule:update')")
    public Result<Void> deleteDimension(@PathVariable Long id) {
        reviewRuleService.deleteDimension(id);
        return Result.success(null);
    }
}
