package com.ga.api.controller.settle;

import com.ga.api.service.settle.IncentivePaymentService;
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.settle.IncentivePaymentResp;
import com.ga.core.vo.settle.IncentivePaymentSaveReq;
import com.ga.core.vo.settle.IncentivePaymentSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "시책 지급 관리")
@RestController
@RequestMapping("/api/incentive-payments")
@RequiredArgsConstructor
public class IncentivePaymentController {

    private final IncentivePaymentService service;

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

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

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

    @PostMapping("/{incentivePayId}/confirm")
    @RequirePermission(menu = "INCENTIVE_PAY", perm = "APPROVE")
    @DataChangeLog(menu = "INCENTIVE_PAY", table = "incentive_payment")
    public ApiResponse<Void> confirm(@PathVariable Long incentivePayId) {
        service.confirm(incentivePayId);
        return ApiResponse.ok();
    }

    @PostMapping("/{incentivePayId}/cancel")
    @RequirePermission(menu = "INCENTIVE_PAY", perm = "UPDATE")
    @DataChangeLog(menu = "INCENTIVE_PAY", table = "incentive_payment")
    public ApiResponse<Void> cancel(@PathVariable Long incentivePayId, @RequestBody Map<String, String> body) {
        service.cancel(incentivePayId, body.get("cancelReason"));
        return ApiResponse.ok();
    }
}
