# 号码分类工具
# 按MEMORY.md最新规则 (2026-04-02)
# 优先级：数字越小越有价值

CATEGORIES = [
    # 1. 圆圆号：无.123457，只能用0689
    {"name": "圆圆号", "cannot_use": ".123457", "must_have": ""},
    # 2. 倒置号：无23457，可用01689必须有1
    {"name": "倒置号", "cannot_use": "23457", "must_have": "1"},
    # 3. 金马王：无12347，可用05689必须有5
    {"name": "金马王", "cannot_use": "12347", "must_have": "5"},
    # 4. 金马号：无2347，可用015689必须有1和5
    {"name": "金马号", "cannot_use": "2347", "must_have": "15"},
    # 5. 金山王：无12457，可用03689必须有3
    {"name": "金山王", "cannot_use": "12457", "must_have": "3"},
    # 6. 天马王：无1247，可用035689必须有3和5
    {"name": "天马王", "cannot_use": "1247", "must_have": "35"},
    # 7. 金山号：无2457，可用013689必须有1和3
    {"name": "金山号", "cannot_use": "2457", "must_have": "13"},
    # 8. 天马号：无247，可用0135689必须有1、3和5
    {"name": "天马号", "cannot_use": "247", "must_have": "135"},
    # 9. 朦胧王：无13457
    {"name": "朦胧王", "cannot_use": "13457", "must_have": ""},
    # 10. 朦胧号：无3457
    {"name": "朦胧号", "cannot_use": "3457", "must_have": ""},
    # 11. 如意号：无1347
    {"name": "如意号", "cannot_use": "1347", "must_have": ""},
    # 12. 钻石号：无347
    {"name": "钻石号", "cannot_use": "347", "must_have": ""},
    # 13. 永恒号：无47
    {"name": "永恒号", "cannot_use": "47", "must_have": ""},
    # 14. 无4号：不包含4
    {"name": "无4号", "cannot_use": "4", "must_have": ""},
    # 15. 通货：含4
    {"name": "通货", "cannot_use": "", "must_have": "4"},
]


def extract_digits(serial: str) -> dict:
    """提取冠字号中的数字部分"""
    if not serial:
        return {"digits": "", "type": "single"}
    
    nums = serial.replace("J", "").replace(",", "").replace(".", "").strip()
    nums = "".join(c for c in nums if c.isdigit())
    
    if nums.endswith("01"):
        return {"digits": nums[:-2], "type": "hundred"}
    elif nums.endswith("1"):
        return {"digits": nums[:-1], "type": "ten"}
    else:
        return {"digits": nums, "type": "single"}


def matches_category(digits: str, category: dict) -> bool:
    cannot_use = category.get("cannot_use", "")
    must_have = category.get("must_have", "")
    
    # 检查不能用的数字
    for n in cannot_use:
        if n in digits:
            return False
    
    # 检查必须有的数字
    if must_have:
        for n in must_have:
            if n not in digits:
                return False
    
    return True


def get_number_category(serial: str) -> str:
    """号码分类函数"""
    if not serial:
        return ""
    
    # 提取数字
    info = extract_digits(serial)
    digits = info["digits"]
    
    if not digits:
        return ""
    
    # 根据类型取对应位数
    digits_type = info["type"]
    if digits_type == "hundred":
        # 标百看后6位
        check_digits = digits[-6:] if len(digits) >= 6 else digits
    elif digits_type == "ten":
        # 标十看后7位
        check_digits = digits[-7:] if len(digits) >= 7 else digits
    else:
        # 散钞看全部
        check_digits = digits
    
    # 按优先级匹配分类
    for category in CATEGORIES:
        if matches_category(check_digits, category):
            return category["name"]
    
    return "通货"


def get_number_category_color(category: str) -> str:
    """获取分类颜色"""
    colors = {
        "圆圆号": "#ef4444",     # 红
        "倒置号": "#f97316",    # 橙
        "金马王": "#eab308",    # 黄
        "金马号": "#84cc16",    # 绿
        "金山王": "#22c55e",    # 深绿
        "天马王": "#14b8a6",   # 青
        "金山号": "#06b6d4",   # 蓝
        "天马号": "#0ea5e9",   # 浅蓝
        "朦胧王": "#6366f1",     # 靛蓝
        "朦胧号": "#8b5cf6",    # 紫
        "如意号": "#a855f7",    # 深紫
        "钻石号": "#d946ef",    # 品红
        "永恒号": "#ec4899",    # 粉红
        "无4号": "#64748b",     # 灰
        "通货": "#9ca3af",     # 浅灰
    }
    return colors.get(category, "#9ca3af")


def get_category_priority(category: str) -> int:
    """获取分类优先级（数字越小越高级）"""
    for i, cat in enumerate(CATEGORIES, 1):
        if cat["name"] == category:
            return i
    return 999