package com.ga.api.controller.settle;

import com.ga.api.service.settle.PeriodCloseService;
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.PeriodCloseResp;
import com.ga.core.vo.settle.PeriodCloseSaveReq;
import com.ga.core.vo.settle.PeriodCloseSearchParam;
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/period-close")
@RequiredArgsConstructor
public class PeriodCloseController {

    private final PeriodCloseService service;

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

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

    @PostMapping
    @RequirePermission(menu = "PERIOD_CLOSE", perm = "CREATE")
    @DataChangeLog(menu = "PERIOD_CLOSE", table = "period_close")
    public ApiResponse<Void> close(@Valid @RequestBody PeriodCloseSaveReq req) {
        service.close(req);
        return ApiResponse.ok();
    }

    @PostMapping("/{closeId}/reopen")
    @RequirePermission(menu = "PERIOD_CLOSE", perm = "UPDATE")
    @DataChangeLog(menu = "PERIOD_CLOSE", table = "period_close")
    public ApiResponse<Void> reopen(@PathVariable Long closeId, @RequestBody Map<String, String> body) {
        service.reopen(closeId, body.get("reopenReason"));
        return ApiResponse.ok();
    }
}
