package com.ga.api.controller.ledger;

import com.ga.api.service.ledger.LedgerService;
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.ledger.*;
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.*;

import java.util.Map;

@Tag(name = "원장 관리")
@RestController
@RequestMapping("/api/ledger")
@RequiredArgsConstructor
public class LedgerController {

    private final LedgerService service;
    private final ExcelService excelService;

    // ===== 모집 =====
    @GetMapping("/recruit")
    @RequirePermission(menu = "LEDGER_RECRUIT", perm = "READ")
    public ApiResponse<PageResponse<LedgerResp>> listRecruit(@ModelAttribute LedgerSearchParam param) {
        return ApiResponse.ok(service.listRecruit(param));
    }

    @GetMapping("/recruit/{ledgerId}")
    @RequirePermission(menu = "LEDGER_RECRUIT", perm = "READ")
    public ApiResponse<LedgerResp> detailRecruit(@PathVariable Long ledgerId) {
        return ApiResponse.ok(service.detailRecruit(ledgerId));
    }

    @GetMapping("/recruit/export")
    @RequirePermission(menu = "LEDGER_RECRUIT", perm = "EXPORT")
    public void exportRecruit(@ModelAttribute LedgerSearchParam param, HttpServletResponse res) {
        excelService.exportLargeExcel("모집수수료원장", LedgerExcelVO.class,
                () -> service.exportRecruitCursor(param), res);
    }

    // ===== 유지 =====
    @GetMapping("/maintain")
    @RequirePermission(menu = "LEDGER_MAINTAIN", perm = "READ")
    public ApiResponse<PageResponse<LedgerResp>> listMaintain(@ModelAttribute LedgerSearchParam param) {
        return ApiResponse.ok(service.listMaintain(param));
    }

    @GetMapping("/maintain/{ledgerId}")
    @RequirePermission(menu = "LEDGER_MAINTAIN", perm = "READ")
    public ApiResponse<LedgerResp> detailMaintain(@PathVariable Long ledgerId) {
        return ApiResponse.ok(service.detailMaintain(ledgerId));
    }

    @GetMapping("/maintain/export")
    @RequirePermission(menu = "LEDGER_MAINTAIN", perm = "EXPORT")
    public void exportMaintain(@ModelAttribute LedgerSearchParam param, HttpServletResponse res) {
        excelService.exportLargeExcel("유지수수료원장", LedgerExcelVO.class,
                () -> service.exportMaintainCursor(param), res);
    }

    // ===== 예외 =====
    @GetMapping("/exception")
    @RequirePermission(menu = "LEDGER_EXCEPTION", perm = "READ")
    public ApiResponse<PageResponse<ExceptionLedgerResp>> listException(@ModelAttribute LedgerSearchParam param) {
        return ApiResponse.ok(service.listException(param));
    }

    @GetMapping("/exception/{ledgerId}")
    @RequirePermission(menu = "LEDGER_EXCEPTION", perm = "READ")
    public ApiResponse<ExceptionLedgerResp> detailException(@PathVariable Long ledgerId) {
        return ApiResponse.ok(service.detailException(ledgerId));
    }

    @PostMapping("/exception")
    @RequirePermission(menu = "LEDGER_EXCEPTION", perm = "CREATE")
    @DataChangeLog(menu = "LEDGER_EXCEPTION", table = "exception_ledger")
    public ApiResponse<Long> createException(@Valid @RequestBody ExceptionLedgerSaveReq req) {
        return ApiResponse.ok(service.createException(req));
    }

    @PutMapping("/exception/{ledgerId}/approve")
    @RequirePermission(menu = "LEDGER_EXCEPTION", perm = "APPROVE")
    @DataChangeLog(menu = "LEDGER_EXCEPTION", table = "exception_ledger")
    public ApiResponse<Void> approveException(@PathVariable Long ledgerId,
                                              @RequestBody Map<String, String> body) {
        service.approveException(ledgerId, body.get("status"));
        return ApiResponse.ok();
    }
}
