package com.ga.api.controller.commission;

import com.ga.api.service.commission.RecognizedPerformanceService;
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/recognized-performances")
@RequiredArgsConstructor
public class RecognizedPerformanceController {

    private final RecognizedPerformanceService service;

    // ── Product Conversion Rate (rule) ────────────────────────────────────────

    @GetMapping("/rates")
    @RequirePermission(menu = "RECOGNIZED_PERF", perm = "READ")
    public ApiResponse<PageResponse<ProductConversionRateResp>> listRates(
            @ModelAttribute ProductConversionRateSearchParam param) {
        return ApiResponse.ok(service.listRates(param));
    }

    @GetMapping("/rates/{rateId}")
    @RequirePermission(menu = "RECOGNIZED_PERF", perm = "READ")
    public ApiResponse<ProductConversionRateResp> detailRate(@PathVariable Long rateId) {
        return ApiResponse.ok(service.detailRate(rateId));
    }

    @PostMapping("/rates")
    @RequirePermission(menu = "RECOGNIZED_PERF", perm = "CREATE")
    @DataChangeLog(menu = "RECOGNIZED_PERF", table = "product_conversion_rate")
    public ApiResponse<Long> createRate(@Valid @RequestBody ProductConversionRateSaveReq req) {
        return ApiResponse.ok(service.createRate(req));
    }

    @PutMapping("/rates/{rateId}")
    @RequirePermission(menu = "RECOGNIZED_PERF", perm = "UPDATE")
    @DataChangeLog(menu = "RECOGNIZED_PERF", table = "product_conversion_rate")
    public ApiResponse<Void> updateRate(@PathVariable Long rateId,
                                        @Valid @RequestBody ProductConversionRateSaveReq req) {
        service.updateRate(rateId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/rates/{rateId}")
    @RequirePermission(menu = "RECOGNIZED_PERF", perm = "DELETE")
    @DataChangeLog(menu = "RECOGNIZED_PERF", table = "product_conversion_rate")
    public ApiResponse<Void> deleteRate(@PathVariable Long rateId) {
        service.deleteRate(rateId);
        return ApiResponse.ok();
    }

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

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

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

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