package com.ga.api.controller.commission;

import com.ga.api.service.commission.ChargebackWaiverService;
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.ChargebackWaiverRequestResp;
import com.ga.core.vo.commission.ChargebackWaiverRequestSaveReq;
import com.ga.core.vo.commission.ChargebackWaiverRequestSearchParam;
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/chargeback-waivers")
@RequiredArgsConstructor
public class ChargebackWaiverController {

    private final ChargebackWaiverService service;

    @GetMapping
    @RequirePermission(menu = "CHARGEBACK_WAIVER", perm = "READ")
    public ApiResponse<PageResponse<ChargebackWaiverRequestResp>> list(
            @ModelAttribute ChargebackWaiverRequestSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{requestId}")
    @RequirePermission(menu = "CHARGEBACK_WAIVER", perm = "READ")
    public ApiResponse<ChargebackWaiverRequestResp> detail(@PathVariable Long requestId) {
        return ApiResponse.ok(service.detail(requestId));
    }

    @PostMapping
    @RequirePermission(menu = "CHARGEBACK_WAIVER", perm = "CREATE")
    @DataChangeLog(menu = "CHARGEBACK_WAIVER", table = "chargeback_waiver_request")
    public ApiResponse<Long> create(@Valid @RequestBody ChargebackWaiverRequestSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PostMapping("/{requestId}/approve")
    @RequirePermission(menu = "CHARGEBACK_WAIVER", perm = "UPDATE")
    @DataChangeLog(menu = "CHARGEBACK_WAIVER", table = "chargeback_waiver_request")
    public ApiResponse<Void> approve(@PathVariable Long requestId) {
        service.approve(requestId);
        return ApiResponse.ok();
    }

    @PostMapping("/{requestId}/reject")
    @RequirePermission(menu = "CHARGEBACK_WAIVER", perm = "UPDATE")
    @DataChangeLog(menu = "CHARGEBACK_WAIVER", table = "chargeback_waiver_request")
    public ApiResponse<Void> reject(@PathVariable Long requestId) {
        service.reject(requestId);
        return ApiResponse.ok();
    }
}
