package com.ga.api.controller.settle;

import com.ga.api.service.settle.PaymentBatchService;
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.PaymentBatchItemUpdateReq;
import com.ga.core.vo.settle.PaymentBatchResp;
import com.ga.core.vo.settle.PaymentBatchSaveReq;
import com.ga.core.vo.settle.PaymentBatchSearchParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "지급대상 배치 관리")
@RestController
@RequestMapping("/api/payment-batches")
@RequiredArgsConstructor
public class PaymentBatchController {

    private final PaymentBatchService service;

    @PostMapping
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "CREATE")
    @DataChangeLog(menu = "PAYMENT_TARGET", table = "payment_batch")
    @Operation(summary = "배치 생성", description = "정산월의 지급대상(payable_amount>0) 건으로 DRAFT 배치를 구성합니다")
    public ApiResponse<Long> create(@Valid @RequestBody PaymentBatchSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @GetMapping
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "READ")
    @Operation(summary = "배치 목록 조회")
    public ApiResponse<PageResponse<PaymentBatchResp>> list(@ModelAttribute PaymentBatchSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{id}")
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "READ")
    @Operation(summary = "배치 상세 조회", description = "배치 헤더 + items 포함")
    public ApiResponse<PaymentBatchResp> detail(@PathVariable Long id) {
        return ApiResponse.ok(service.detail(id));
    }

    @PutMapping("/{id}/items/{itemId}")
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT_TARGET", table = "payment_batch_item")
    @Operation(summary = "지급대상건 포함/제외 수정", description = "DRAFT 배치만 수정 가능합니다")
    public ApiResponse<Void> updateItem(@PathVariable Long id,
                                        @PathVariable Long itemId,
                                        @Valid @RequestBody PaymentBatchItemUpdateReq req) {
        service.updateItem(id, itemId, req);
        return ApiResponse.ok();
    }

    @PutMapping("/{id}/finalize")
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "EXECUTE")
    @DataChangeLog(menu = "PAYMENT_TARGET", table = "payment_batch")
    @Operation(summary = "배치 확정", description = "DRAFT → FINALIZED. 포함건이 없으면 확정 불가")
    public ApiResponse<Void> finalize(@PathVariable Long id) {
        service.finalize(id);
        return ApiResponse.ok();
    }

    @PutMapping("/{id}/cancel")
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT_TARGET", table = "payment_batch")
    @Operation(summary = "배치 취소")
    public ApiResponse<Void> cancel(@PathVariable Long id) {
        service.cancel(id);
        return ApiResponse.ok();
    }

    @GetMapping("/{id}/export")
    @RequirePermission(menu = "PAYMENT_TARGET", perm = "READ")
    @Operation(summary = "배치 items 엑셀 다운로드")
    public void export(@PathVariable Long id, HttpServletResponse response) {
        service.export(id, response);
    }
}
