"""Chinese user-facing messages for watch probe failures."""

from __future__ import annotations

_EXACT: dict[str, str] = {
    "parse_error": "探针返回数据无法解析",
    "psutil missing": "子机未安装 psutil",
    "ssh_probe_failed": "SSH 探针脚本执行失败",
    "SSH watch probe failed": "SSH 监测探针失败",
    "SSH probe failed": "SSH 探针失败",
    "timeout": "连接超时",
    "探针失败": "探针失败",
    "无 Agent 且 SSH 探针失败": "无 Agent 且 SSH 探针失败",
    "服务器不存在": "服务器不存在",
}


def watch_probe_error_zh(error: str | None, *, status: str | None = None) -> str | None:
    """Map probe error text to Chinese for UI / audit display."""
    if not error:
        if status == "parse_error":
            return "探针返回数据无法解析"
        if status == "ssh_timeout":
            return "SSH 连接超时"
        if status == "ssh_auth":
            return "SSH 认证失败"
        if status == "no_cred":
            return "需要 Agent 或 SSH 凭据"
        if status == "redis_stale":
            return "Agent 心跳已过期"
        if status == "offline":
            return "目标服务器离线或不可达"
        return None

    text = error.strip()
    if text in _EXACT:
        return _EXACT[text]

    lower = text.lower()
    if lower.startswith("parse_error"):
        detail = text.split(":", 1)[1].strip() if ":" in text else ""
        if detail in _EXACT:
            return f"探针返回数据无法解析：{_EXACT[detail]}"
        if detail:
            return f"探针返回数据无法解析：{detail}"
        return "探针返回数据无法解析"

    if "timed out" in lower or lower.endswith("timeout"):
        return f"SSH 连接超时：{text}"
    if "permission denied" in lower or "authentication failed" in lower:
        return f"SSH 认证失败：{text}"
    if "psutil missing" in lower:
        return "子机未安装 psutil"
    if "ssh_probe_failed" in lower or "ssh watch probe failed" in lower:
        return "SSH 探针脚本执行失败"

    # Already Chinese or mixed — return as-is if contains CJK
    if any("\u4e00" <= ch <= "\u9fff" for ch in text):
        return text

    return f"探针异常：{text}"
