package com.ga.api.controller.commission;

import com.ga.api.service.commission.AdvancePaymentService;
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.AdvancePaymentResp;
import com.ga.core.vo.commission.AdvancePaymentSaveReq;
import com.ga.core.vo.commission.AdvancePaymentSearchParam;
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/advance-payments")
@RequiredArgsConstructor
public class AdvancePaymentController {

    private final AdvancePaymentService service;

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

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

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

    @PostMapping("/{advanceId}/waive")
    @RequirePermission(menu = "ADVANCE_PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "ADVANCE_PAYMENT", table = "advance_payment")
    public ApiResponse<Void> waive(@PathVariable Long advanceId) {
        service.waive(advanceId);
        return ApiResponse.ok();
    }
}
