package com.ga.api.controller.settle;

import com.ga.api.service.settle.TaxInvoiceService;
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.TaxInvoiceResp;
import com.ga.core.vo.settle.TaxInvoiceSaveReq;
import com.ga.core.vo.settle.TaxInvoiceSearchParam;
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/tax-invoices")
@RequiredArgsConstructor
public class TaxInvoiceController {

    private final TaxInvoiceService service;

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

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

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

    @PostMapping("/{invoiceId}/cancel")
    @RequirePermission(menu = "TAX_INVOICE", perm = "UPDATE")
    @DataChangeLog(menu = "TAX_INVOICE", table = "tax_invoice")
    public ApiResponse<Void> cancel(@PathVariable Long invoiceId, @RequestBody Map<String, String> body) {
        service.cancel(invoiceId, body.get("cancelReason"));
        return ApiResponse.ok();
    }
}
