package com.ga.api.controller.settle;

import com.ga.api.service.settle.CommissionDisputeService;
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.settle.CommissionDisputeResp;
import com.ga.core.vo.settle.CommissionDisputeSaveReq;
import com.ga.core.vo.settle.CommissionDisputeSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "수수료 이의신청 관리")
@RestController
@RequestMapping("/api/disputes")
@RequiredArgsConstructor
public class CommissionDisputeController {

    private final CommissionDisputeService service;

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

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

    @PostMapping
    @RequirePermission(menu = "DISPUTE", perm = "CREATE")
    @DataChangeLog(menu = "DISPUTE", table = "commission_dispute")
    public ApiResponse<Long> create(@Valid @RequestBody CommissionDisputeSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PostMapping("/{disputeId}/review")
    @RequirePermission(menu = "DISPUTE", perm = "UPDATE")
    @DataChangeLog(menu = "DISPUTE", table = "commission_dispute")
    public ApiResponse<Void> startReview(@PathVariable Long disputeId) {
        service.startReview(disputeId);
        return ApiResponse.ok();
    }

    @PostMapping("/{disputeId}/approve")
    @RequirePermission(menu = "DISPUTE", perm = "APPROVE")
    @DataChangeLog(menu = "DISPUTE", table = "commission_dispute")
    public ApiResponse<Void> approve(@PathVariable Long disputeId, @RequestBody Map<String, Object> body) {
        String resolution = (String) body.get("resolution");
        BigDecimal adjustmentAmount = body.get("adjustmentAmount") != null
                ? new BigDecimal(body.get("adjustmentAmount").toString()) : null;
        Long correctionId = body.get("correctionId") != null
                ? Long.valueOf(body.get("correctionId").toString()) : null;
        service.approve(disputeId, resolution, adjustmentAmount, correctionId);
        return ApiResponse.ok();
    }

    @PostMapping("/{disputeId}/reject")
    @RequirePermission(menu = "DISPUTE", perm = "APPROVE")
    @DataChangeLog(menu = "DISPUTE", table = "commission_dispute")
    public ApiResponse<Void> reject(@PathVariable Long disputeId, @RequestBody Map<String, String> body) {
        service.reject(disputeId, body.get("resolution"));
        return ApiResponse.ok();
    }
}
