package com.ga.api.controller.tax;

import com.ga.api.service.tax.WithholdingTaxReportService;
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.tax.WithholdingTaxReportResp;
import com.ga.core.vo.tax.WithholdingTaxReportSaveReq;
import com.ga.core.vo.tax.WithholdingTaxReportSearchParam;
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.*;

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

@Tag(name = "원천세 분기신고")
@RestController
@RequestMapping("/api/withholding-tax-reports")
@RequiredArgsConstructor
public class WithholdingTaxReportController {

    private final WithholdingTaxReportService service;

    @GetMapping
    @Operation(summary = "원천세 분기신고 목록 조회")
    @RequirePermission(menu = "WITHHOLDING_TAX", perm = "READ")
    public ApiResponse<PageResponse<WithholdingTaxReportResp>> list(
            @ModelAttribute WithholdingTaxReportSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{id}")
    @Operation(summary = "원천세 분기신고 상세 조회")
    @RequirePermission(menu = "WITHHOLDING_TAX", perm = "READ")
    public ApiResponse<WithholdingTaxReportResp> detail(@PathVariable Long id) {
        return ApiResponse.ok(service.detail(id));
    }

    @PostMapping("/regenerate")
    @Operation(summary = "분기 원천세 재집계")
    @RequirePermission(menu = "WITHHOLDING_TAX", perm = "EXECUTE")
    @DataChangeLog(menu = "WITHHOLDING_TAX", table = "withholding_tax_report")
    public ApiResponse<Integer> regenerate(@Valid @RequestBody WithholdingTaxReportSaveReq req) {
        return ApiResponse.ok(service.regenerate(req));
    }

    @PutMapping("/complete")
    @Operation(summary = "원천세 분기신고 제출 완료 처리")
    @RequirePermission(menu = "WITHHOLDING_TAX", perm = "EXECUTE")
    @DataChangeLog(menu = "WITHHOLDING_TAX", table = "withholding_tax_report")
    public ApiResponse<Void> complete(@RequestBody Map<String, List<Long>> body) {
        service.complete(body.get("ids"));
        return ApiResponse.ok();
    }
}
