package com.ga.api.controller.commission;

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

    private final RecruiterCommissionService service;

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

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

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

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

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

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

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

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

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

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