package com.ga.api.controller.commission;

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

    private final RenewalCommissionService service;

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

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

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

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

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

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

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

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