package com.jeesharp.business.modules.mall.sync;

import com.jeesharp.business.modules.mall.entity.MallOrder;
import com.jeesharp.business.modules.mall.enums.MallOrderStatusEnum;
import com.jeesharp.business.modules.mall.service.MallOrderService;
import com.jeesharp.business.modules.trade.api.TradeOrderFacadeApi;
import com.jeesharp.business.modules.trade.entity.TradeOrder;
import com.jeesharp.business.modules.trade.enums.TradeBizTypeEnum;
import com.jeesharp.business.modules.trade.enums.TradeOrderStatusEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 商城订单与统一交易订单影子核对。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Service
@AllArgsConstructor
public class MallTradeOrderShadowService {

    private final MallOrderService mallOrderService;
    private final TradeOrderFacadeApi tradeOrderFacadeApi;

    /**
     * @return {@code null} 表示一致或未配置统一单；否则差异说明
     */
    public String verifyMallVsTrade(String mallOrderId) {
        MallOrder mall = mallOrderService.get(mallOrderId);
        if (mall == null) {
            return "商城订单不存在";
        }
        if (mall.getUnifiedOrderId() == null || mall.getUnifiedOrderId().isBlank()) {
            return null;
        }
        TradeOrder trade = tradeOrderFacadeApi.get(mall.getUnifiedOrderId());
        if (trade == null) {
            return "统一单不存在 unifiedOrderId=" + mall.getUnifiedOrderId();
        }
        if (!TradeBizTypeEnum.MALL.getValue().equals(trade.getBizType())) {
            return "统一单 biz_type 不一致 expect=MALL actual=" + trade.getBizType();
        }
        if (!tradeStatusMatchesMall(trade.getStatus(), mall.getStatus())) {
            return "统一单与商城单状态不一致 trade=" + trade.getStatus() + " mall=" + mall.getStatus();
        }
        if (mall.getMemberId() != null && trade.getMemberId() != null && !mall.getMemberId().equals(trade.getMemberId())) {
            return "会员ID不一致";
        }
        if (trade.getVerticalOrderId() != null && !trade.getVerticalOrderId().equals(mall.getId())) {
            return "vertical_order_id 与商城单ID不一致";
        }
        BigDecimal mallPay = resolvePayAmount(mall);
        if (mallPay != null && trade.getPayAmount() != null && mallPay.compareTo(trade.getPayAmount()) != 0) {
            return "金额不一致 商城=" + mallPay + " 统一单=" + trade.getPayAmount();
        }
        return null;
    }

    private static boolean tradeStatusMatchesMall(Integer tradeStatus, Integer mallStatus) {
        if (tradeStatus == null || mallStatus == null) {
            return false;
        }
        MallOrderStatusEnum mall = MallOrderStatusEnum.of(mallStatus);
        if (mall == null) {
            return true;
        }
        return switch (mall) {
            case PENDING_PAY -> TradeOrderStatusEnum.PENDING_PAY.getValue().equals(tradeStatus);
            case PAID, TO_FULFILL -> TradeOrderStatusEnum.PAID.getValue().equals(tradeStatus);
            case FULFILLING -> TradeOrderStatusEnum.FULFILLING.getValue().equals(tradeStatus);
            case FULFILLED -> TradeOrderStatusEnum.FULFILLED.getValue().equals(tradeStatus)
                    || TradeOrderStatusEnum.CLOSED.getValue().equals(tradeStatus);
            case REFUNDING -> TradeOrderStatusEnum.REFUNDING.getValue().equals(tradeStatus);
            case REFUNDED -> TradeOrderStatusEnum.REFUNDED.getValue().equals(tradeStatus);
            case CANCELED -> TradeOrderStatusEnum.CANCELLED.getValue().equals(tradeStatus);
            case CLOSED -> TradeOrderStatusEnum.CLOSED.getValue().equals(tradeStatus)
                    || TradeOrderStatusEnum.CANCELLED.getValue().equals(tradeStatus);
            default -> true;
        };
    }

    private static BigDecimal resolvePayAmount(MallOrder mall) {
        if (mall.getPayAmount() != null) {
            return mall.getPayAmount();
        }
        return mall.getTotalAmount();
    }
}
