package com.ga.api.controller.commission;

import com.ga.api.service.commission.ClawbackReceivableService;
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.common.util.SecurityUtil;
import com.ga.core.vo.commission.ClawbackReceivableResp;
import com.ga.core.vo.commission.ClawbackReceivableSaveReq;
import com.ga.core.vo.commission.ClawbackReceivableSearchParam;
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.List;
import java.util.Map;

@Tag(name = "환수채권 관리")
@RestController
@RequestMapping("/api/clawback-receivables")
@RequiredArgsConstructor
public class ClawbackReceivableController {

    private final ClawbackReceivableService service;

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

    @GetMapping("/summary")
    @RequirePermission(menu = "CLAWBACK_RECEIVABLE", perm = "READ")
    public ApiResponse<List<Map<String, Object>>> summary(
            @ModelAttribute ClawbackReceivableSearchParam param) {
        return ApiResponse.ok(service.summary(param));
    }

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

    @PutMapping("/{id}/write-off")
    @RequirePermission(menu = "CLAWBACK_RECEIVABLE", perm = "UPDATE")
    @DataChangeLog(menu = "CLAWBACK_RECEIVABLE", table = "clawback_receivable")
    public ApiResponse<Void> writeOff(@PathVariable Long id,
                                      @Valid @RequestBody ClawbackReceivableSaveReq req) {
        service.writeOff(id, req, SecurityUtil.getCurrentUserId());
        return ApiResponse.ok();
    }

    @GetMapping("/export")
    @RequirePermission(menu = "CLAWBACK_RECEIVABLE", perm = "EXPORT")
    public void export(@ModelAttribute ClawbackReceivableSearchParam param,
                       HttpServletResponse response) {
        service.export(param, response);
    }
}
