package com.ga.api.controller.commission;

import com.ga.api.service.commission.BranchAllowanceService;
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/branch-allowances")
@RequiredArgsConstructor
public class BranchAllowanceController {

    private final BranchAllowanceService service;

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

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

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

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

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

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

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

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

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

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