package com.ga.api.controller.commission;

import com.ga.api.service.commission.GradeEvaluationService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.commission.AgentGradeEvaluationResp;
import com.ga.core.vo.commission.AgentGradeEvaluationSaveReq;
import com.ga.core.vo.commission.AgentGradeEvaluationSearchParam;
import com.ga.core.vo.commission.GradeEvalRuleResp;
import com.ga.core.vo.commission.GradeEvalRuleSaveReq;
import com.ga.core.vo.commission.GradeEvalRuleSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "등급평가")
@RestController
@RequestMapping("/api/grade-evaluations")
@RequiredArgsConstructor
public class GradeEvaluationController {

    private final GradeEvaluationService service;

    // ── Rule ──────────────────────────────────────────────────────────────────

    @GetMapping("/rules")
    @RequirePermission(menu = "GRADE_EVAL", perm = "READ")
    public ApiResponse<PageResponse<GradeEvalRuleResp>> listRules(@ModelAttribute GradeEvalRuleSearchParam param) {
        return ApiResponse.ok(service.listRules(param));
    }

    @GetMapping("/rules/{ruleId}")
    @RequirePermission(menu = "GRADE_EVAL", perm = "READ")
    public ApiResponse<GradeEvalRuleResp> detailRule(@PathVariable Long ruleId) {
        return ApiResponse.ok(service.detailRule(ruleId));
    }

    @PostMapping("/rules")
    @RequirePermission(menu = "GRADE_EVAL", perm = "CREATE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "grade_eval_rule")
    public ApiResponse<Long> createRule(@Valid @RequestBody GradeEvalRuleSaveReq req) {
        return ApiResponse.ok(service.createRule(req));
    }

    @PutMapping("/rules/{ruleId}")
    @RequirePermission(menu = "GRADE_EVAL", perm = "UPDATE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "grade_eval_rule")
    public ApiResponse<Void> updateRule(@PathVariable Long ruleId,
                                        @Valid @RequestBody GradeEvalRuleSaveReq req) {
        service.updateRule(ruleId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/rules/{ruleId}")
    @RequirePermission(menu = "GRADE_EVAL", perm = "DELETE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "grade_eval_rule")
    public ApiResponse<Void> deleteRule(@PathVariable Long ruleId) {
        service.deleteRule(ruleId);
        return ApiResponse.ok();
    }

    // ── Evaluation ────────────────────────────────────────────────────────────

    @GetMapping
    @RequirePermission(menu = "GRADE_EVAL", perm = "READ")
    public ApiResponse<PageResponse<AgentGradeEvaluationResp>> listEvals(
            @ModelAttribute AgentGradeEvaluationSearchParam param) {
        return ApiResponse.ok(service.listEvals(param));
    }

    @GetMapping("/{evalId}")
    @RequirePermission(menu = "GRADE_EVAL", perm = "READ")
    public ApiResponse<AgentGradeEvaluationResp> detailEval(@PathVariable Long evalId) {
        return ApiResponse.ok(service.detailEval(evalId));
    }

    @PostMapping
    @RequirePermission(menu = "GRADE_EVAL", perm = "CREATE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "agent_grade_evaluation")
    public ApiResponse<Long> createEval(@Valid @RequestBody AgentGradeEvaluationSaveReq req) {
        return ApiResponse.ok(service.createEval(req));
    }

    @PostMapping("/{evalId}/apply")
    @RequirePermission(menu = "GRADE_EVAL", perm = "UPDATE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "agent_grade_evaluation")
    public ApiResponse<Void> applyEval(@PathVariable Long evalId) {
        service.applyEval(evalId);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{evalId}")
    @RequirePermission(menu = "GRADE_EVAL", perm = "DELETE")
    @DataChangeLog(menu = "GRADE_EVAL", table = "agent_grade_evaluation")
    public ApiResponse<Void> deleteEval(@PathVariable Long evalId) {
        service.deleteEval(evalId);
        return ApiResponse.ok();
    }
}
