package com.ga.api.controller.commission;

import com.ga.api.service.commission.LifeSupportService;
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/life-supports")
@RequiredArgsConstructor
public class LifeSupportController {

    private final LifeSupportService service;

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

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

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

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

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

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

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

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

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

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