package com.ga.api.controller.settle;

import com.ga.api.service.settle.TerminationSettlementService;
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.TerminationSettlementResp;
import com.ga.core.vo.settle.TerminationSettlementSaveReq;
import com.ga.core.vo.settle.TerminationSettlementSearchParam;
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/termination-settlements")
@RequiredArgsConstructor
public class TerminationSettlementController {

    private final TerminationSettlementService service;

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

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

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

    @PutMapping("/{termSettleId}")
    @RequirePermission(menu = "TERM_SETTLE", perm = "UPDATE")
    @DataChangeLog(menu = "TERM_SETTLE", table = "termination_settlement")
    public ApiResponse<Void> update(@PathVariable Long termSettleId, @Valid @RequestBody TerminationSettlementSaveReq req) {
        service.update(termSettleId, req);
        return ApiResponse.ok();
    }

    @PostMapping("/process")
    @RequirePermission(menu = "TERM_SETTLE", perm = "CREATE")
    @DataChangeLog(menu = "TERM_SETTLE", table = "termination_settlement")
    public ApiResponse<Long> process(@Valid @RequestBody TerminationSettlementSaveReq req) {
        return ApiResponse.ok(service.process(req));
    }

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