package com.ga.api.controller.settle;

import com.ga.api.service.settle.SettleService;
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.SettleMasterResp;
import com.ga.core.vo.settle.SettleSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "정산 관리")
@RestController
@RequestMapping("/api/settle")
@RequiredArgsConstructor
public class SettleController {

    private final SettleService service;

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

    @GetMapping("/{agentId}/months/{settleMonth}")
    @RequirePermission(menu = "SETTLE_LIST", perm = "READ")
    public ApiResponse<SettleMasterResp> detail(@PathVariable Long agentId,
                                                @PathVariable String settleMonth) {
        return ApiResponse.ok(service.detail(agentId, settleMonth));
    }

    @GetMapping("/summary")
    @RequirePermission(menu = "SETTLE_LIST", perm = "READ")
    public ApiResponse<Map<String, Object>> summary(@RequestParam String settleMonth) {
        return ApiResponse.ok(service.summary(settleMonth));
    }

    @GetMapping("/org-summary")
    @RequirePermission(menu = "SETTLE_LIST", perm = "READ")
    public ApiResponse<List<Map<String, Object>>> orgSummary(@RequestParam String settleMonth) {
        return ApiResponse.ok(service.orgSummary(settleMonth));
    }

    @PutMapping("/{settleId}/confirm")
    @RequirePermission(menu = "SETTLE_LIST", perm = "APPROVE")
    @DataChangeLog(menu = "SETTLE_LIST", table = "settle_master")
    public ApiResponse<Void> confirm(@PathVariable Long settleId) {
        service.confirm(settleId);
        return ApiResponse.ok();
    }

    @PutMapping("/{settleId}/hold")
    @RequirePermission(menu = "SETTLE_LIST", perm = "APPROVE")
    @DataChangeLog(menu = "SETTLE_LIST", table = "settle_master")
    public ApiResponse<Void> hold(@PathVariable Long settleId, @RequestBody Map<String, String> body) {
        service.hold(settleId, body.get("reason"), body.get("reasonCode"));
        return ApiResponse.ok();
    }

    @PutMapping("/{settleId}/release")
    @RequirePermission(menu = "SETTLE_LIST", perm = "APPROVE")
    @DataChangeLog(menu = "SETTLE_LIST", table = "settle_master")
    public ApiResponse<Void> release(@PathVariable Long settleId) {
        service.release(settleId);
        return ApiResponse.ok();
    }
}
