package com.ga.api.controller.commission;

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

    private final SettlingSupportService service;

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

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

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

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

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

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

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

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

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

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