package com.ga.api.controller.commission;

import com.ga.api.service.commission.HqSettleService;
import com.ga.api.service.commission.HqSettleService.HqSummaryResp;
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.HqSettleItemResp;
import com.ga.core.vo.commission.HqSettleItemSaveReq;
import com.ga.core.vo.commission.HqSettleItemSearchParam;
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/hq-settles")
@RequiredArgsConstructor
public class HqSettleController {

    private final HqSettleService service;

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

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

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

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

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

    @DeleteMapping("/{itemId}")
    @RequirePermission(menu = "HQ_SETTLE", perm = "DELETE")
    @DataChangeLog(menu = "HQ_SETTLE", table = "hq_settle_item")
    public ApiResponse<Void> delete(@PathVariable Long itemId) {
        service.delete(itemId);
        return ApiResponse.ok();
    }
}
