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

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.biz.homework.dto.CreateHomeworkReviewRuleDto;
import com.competition.modules.biz.homework.entity.BizHomeworkReviewRule;
import com.competition.modules.biz.homework.service.IHomeworkReviewRuleService;
import com.competition.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("/homework/review-rules")
@RequiredArgsConstructor
public class HomeworkReviewRuleController {

    private final IHomeworkReviewRuleService homeworkReviewRuleService;

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

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

    @PostMapping
    @RequirePermission("homework:update")
    @Operation(summary = "创建作业评审规则")
    public Result<BizHomeworkReviewRule> create(@Valid @RequestBody CreateHomeworkReviewRuleDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkReviewRuleService.create(dto, tenantId));
    }

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

    @PatchMapping("/{id}")
    @RequirePermission("homework:update")
    @Operation(summary = "更新作业评审规则")
    public Result<BizHomeworkReviewRule> update(@PathVariable Long id, @RequestBody CreateHomeworkReviewRuleDto dto) {
        return Result.success(homeworkReviewRuleService.update(id, dto));
    }

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