package com.ga.api.controller.accounting;

import com.ga.api.service.accounting.ContractAccountingService;
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.accounting.ContractAccountingEntryResp;
import com.ga.core.vo.accounting.ContractAccountingEntrySearchParam;
import io.swagger.v3.oas.annotations.Operation;
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/accounting-entries")
@RequiredArgsConstructor
public class ContractAccountingController {

    private final ContractAccountingService service;

    @GetMapping
    @Operation(summary = "분개 엔트리 목록 조회")
    @RequirePermission(menu = "ACCOUNTING_JOURNAL", perm = "READ")
    public ApiResponse<PageResponse<ContractAccountingEntryResp>> list(
            @ModelAttribute ContractAccountingEntrySearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @PostMapping("/generate")
    @Operation(summary = "자동 분개 생성 (paymentIds 또는 settleMonth)")
    @RequirePermission(menu = "ACCOUNTING_JOURNAL", perm = "CREATE")
    @DataChangeLog(menu = "ACCOUNTING_JOURNAL", table = "contract_accounting_entry")
    public ApiResponse<Integer> generate(@RequestBody GenerateEntriesReq req) {
        return ApiResponse.ok(service.generateFromPayments(req));
    }

    @PutMapping("/close")
    @Operation(summary = "분개 마감 처리")
    @RequirePermission(menu = "ACCOUNTING_JOURNAL", perm = "UPDATE")
    @DataChangeLog(menu = "ACCOUNTING_JOURNAL", table = "contract_accounting_entry")
    public ApiResponse<Integer> close(@Valid @RequestBody CloseJournalReq req) {
        return ApiResponse.ok(service.closeJournal(req));
    }
}
