package com.ga.api.controller.commission;

import com.ga.api.service.commission.AwardService;
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.*;
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/awards")
@RequiredArgsConstructor
public class AwardController {

    private final AwardService service;

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

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

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

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

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

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

    // ── Ledger ────────────────────────────────────────────────────────────────

    @GetMapping("/ledgers")
    @RequirePermission(menu = "AWARD", perm = "READ")
    public ApiResponse<PageResponse<AwardLedgerResp>> listLedgers(
            @ModelAttribute AwardLedgerSearchParam param) {
        return ApiResponse.ok(service.listLedgers(param));
    }

    @GetMapping("/ledgers/{ledgerId}")
    @RequirePermission(menu = "AWARD", perm = "READ")
    public ApiResponse<AwardLedgerResp> detailLedger(@PathVariable Long ledgerId) {
        return ApiResponse.ok(service.detailLedger(ledgerId));
    }

    @PostMapping("/ledgers")
    @RequirePermission(menu = "AWARD", perm = "CREATE")
    @DataChangeLog(menu = "AWARD", table = "award_ledger")
    public ApiResponse<Long> createLedger(@Valid @RequestBody AwardLedgerSaveReq req) {
        return ApiResponse.ok(service.createLedger(req));
    }
}
