package com.ga.api.controller.commission;

import com.ga.api.service.commission.StepCloseService;
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.StepCloseResp;
import com.ga.core.vo.commission.StepCloseSaveReq;
import com.ga.core.vo.commission.StepCloseSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "단계마감")
@RestController
@RequestMapping("/api/step-closes")
@RequiredArgsConstructor
public class StepCloseController {

    private final StepCloseService service;

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

    @GetMapping("/by-month")
    @RequirePermission(menu = "STEP_CLOSE", perm = "READ")
    public ApiResponse<List<StepCloseResp>> listByMonth(@RequestParam String settleMonth) {
        return ApiResponse.ok(service.listByMonth(settleMonth));
    }

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

    @PostMapping
    @RequirePermission(menu = "STEP_CLOSE", perm = "UPDATE")
    @DataChangeLog(menu = "STEP_CLOSE", table = "settlement_step_close")
    public ApiResponse<Long> create(@Valid @RequestBody StepCloseSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PostMapping("/{stepCloseId}/close")
    @RequirePermission(menu = "STEP_CLOSE", perm = "APPROVE")
    @DataChangeLog(menu = "STEP_CLOSE", table = "settlement_step_close")
    public ApiResponse<Void> close(@PathVariable Long stepCloseId) {
        service.close(stepCloseId);
        return ApiResponse.ok();
    }

    @PostMapping("/{stepCloseId}/reopen")
    @RequirePermission(menu = "STEP_CLOSE", perm = "APPROVE")
    @DataChangeLog(menu = "STEP_CLOSE", table = "settlement_step_close")
    public ApiResponse<Void> reopen(@PathVariable Long stepCloseId) {
        service.reopen(stepCloseId);
        return ApiResponse.ok();
    }
}
