package com.ga.api.controller.income;

import com.ga.api.service.income.YearEndStatementService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.excel.ExcelService;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.income.YearEndStatementExcelVO;
import com.ga.core.vo.income.YearEndStatementResp;
import com.ga.core.vo.income.YearEndStatementSaveReq;
import com.ga.core.vo.income.YearEndStatementSearchParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "연말 지급명세서")
@RestController
@RequestMapping("/api/year-end-statements")
@RequiredArgsConstructor
public class YearEndStatementController {

    private final YearEndStatementService service;
    private final ExcelService excelService;

    @GetMapping
    @Operation(summary = "연말 지급명세서 목록")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "READ")
    public ApiResponse<PageResponse<YearEndStatementResp>> list(@ModelAttribute YearEndStatementSearchParam p) {
        return ApiResponse.ok(service.list(p));
    }

    @GetMapping("/{id}")
    @Operation(summary = "연말 지급명세서 상세")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "READ")
    public ApiResponse<YearEndStatementResp> detail(@PathVariable Long id) {
        return ApiResponse.ok(service.detail(id));
    }

    @PostMapping("/regenerate")
    @Operation(summary = "연도별 재집계 (UPSERT)")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "EXECUTE")
    @DataChangeLog(menu = "YEAR_END_STMT", table = "year_end_statement")
    public ApiResponse<Integer> regenerate(@RequestBody YearEndStatementSaveReq req) {
        return ApiResponse.ok(service.regenerate(req));
    }

    @PostMapping("/{id}/generate")
    @Operation(summary = "단건 파일 발급")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "EXECUTE")
    @DataChangeLog(menu = "YEAR_END_STMT", table = "year_end_statement")
    public ApiResponse<Void> generate(@PathVariable Long id, @RequestBody(required = false) Map<String, String> body) {
        String format = body == null ? null : body.get("fileFormat");
        service.generate(id, format);
        return ApiResponse.ok();
    }

    @PutMapping("/send")
    @Operation(summary = "발송 처리")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "UPDATE")
    @DataChangeLog(menu = "YEAR_END_STMT", table = "year_end_statement")
    public ApiResponse<Void> send(@RequestBody YearEndStatementSaveReq req) {
        service.send(req);
        return ApiResponse.ok();
    }

    @GetMapping("/export")
    @Operation(summary = "엑셀 다운로드")
    @RequirePermission(menu = "YEAR_END_STMT", perm = "EXPORT")
    public void export(@ModelAttribute YearEndStatementSearchParam p, HttpServletResponse res) {
        excelService.exportLargeExcel(
                "year-end-statement.xlsx",
                YearEndStatementExcelVO.class,
                () -> service.exportCursor(p),
                res);
    }
}
