package com.ga.api.controller.commission;

import com.ga.api.service.commission.ReferralCommissionService;
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/referral-commissions")
@RequiredArgsConstructor
public class ReferralCommissionController {

    private final ReferralCommissionService service;

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

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

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

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

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

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

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

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

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

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