package com.ruoyi.jarvis.service.goofish;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 闲管家开放平台：订单状态、退款状态中文说明（与 Apifox 订单列表 schema 一致）
 */
public final class GoofishStatusLabels {

    private static final Map<Integer, String> ORDER = new HashMap<>();
    private static final Map<Integer, String> REFUND = new HashMap<>();

    static {
        ORDER.put(11, "待付款");
        ORDER.put(12, "待发货");
        ORDER.put(21, "已发货");
        ORDER.put(22, "已完成");
        ORDER.put(23, "已退款");
        ORDER.put(24, "已关闭");
        REFUND.put(0, "未申请退款");
        REFUND.put(1, "待商家处理");
        REFUND.put(2, "待买家退货");
        REFUND.put(3, "待商家收货");
        REFUND.put(4, "退款关闭");
        REFUND.put(5, "退款成功");
        REFUND.put(6, "已拒绝退款");
        REFUND.put(8, "待确认退货地址");
    }

    private GoofishStatusLabels() {
    }

    /** 仅中文；未知时返回 null */
    public static String orderStatusLabel(Integer s) {
        if (s == null) {
            return null;
        }
        return ORDER.get(s);
    }

    /** 仅中文；未知时返回 null */
    public static String refundStatusLabel(Integer s) {
        if (s == null) {
            return null;
        }
        return REFUND.get(s);
    }

    /**
     * 用于日志/企微：优先中文，未知时带码便于排查
     */
    public static String orderStatusHuman(Integer s) {
        String t = orderStatusLabel(s);
        if (t != null) {
            return t;
        }
        if (s == null) {
            return "—";
        }
        return "未识别状态(" + s + ")";
    }

    public static String refundStatusHuman(Integer s) {
        String t = refundStatusLabel(s);
        if (t != null) {
            return t;
        }
        if (s == null) {
            return "—";
        }
        return "未识别状态(" + s + ")";
    }

    /**
     * 变化摘要：A→B，两边均为人话
     */
    public static String orderStatusChange(Integer from, Integer to) {
        return orderStatusHuman(from) + " → " + orderStatusHuman(to);
    }

    /**
     * 快照/首行展示：待发阶段注明已付款语义（开放平台码 12 即待发货）。
     */
    public static String orderStatusHumanForNotify(Integer s) {
        if (Objects.equals(s, 12)) {
            return "待发货（已付款）";
        }
        return orderStatusHuman(s);
    }

    /**
     * 企微通知用变化文案：付款完成单独写「已付款（待发货）」。
     */
    public static String orderStatusChangeForNotify(Integer from, Integer to) {
        if (Objects.equals(from, 11) && Objects.equals(to, 12)) {
            return orderStatusHuman(11) + " → 已付款（待发货）";
        }
        return orderStatusHuman(from) + " → " + orderStatusHumanForNotify(to);
    }

    /**
     * 是否与「付款、待发、在途、终态退款/完成/关闭」相关，从而值得推企微（排除已由 SHIP 覆盖的待发→已发）。
     */
    public static boolean isWxNotifiableOrderStatusChange(Integer from, Integer to) {
        if (Objects.equals(from, to)) {
            return false;
        }
        if (Objects.equals(from, 12) && Objects.equals(to, 21)) {
            return false;
        }
        int[] anchors = {11, 12, 21, 22, 23, 24};
        for (int code : anchors) {
            if (Objects.equals(from, code) || Objects.equals(to, code)) {
                return true;
            }
        }
        return false;
    }

    public static String refundStatusChange(Integer from, Integer to) {
        return refundStatusHuman(from) + " → " + refundStatusHuman(to);
    }
}
