"""
agent67.tools.safety — 安全机制
"""

DANGEROUS_PATTERNS = [
    "rm -rf /", "rm -rf ~", "rm -rf .", "sudo rm -rf",
    "dd if=", "mkfs", "format c:",
    "> /dev/sd", ":(){ :|:& };:",  # fork bomb
    "chmod -R 777 /", "wget -O- | sh", "curl | sh",
    "shutdown", "reboot", "halt",
    "diskutil eraseDisk", "diskutil partitionDisk",
    "launchctl unload", "csrutil disable",
    "defaults delete /",
]

HIGH_RISK_KEYWORDS = [
    "sudo", "rm ", "kill ", "pkill", "killall",
    "mv /", "chmod", "chown",
    "brew uninstall", "pip uninstall",
    "npm uninstall -g",
]


def is_dangerous(cmd: str) -> bool:
    """检查命令是否危险（绝对禁止执行）"""
    cmd_lower = cmd.lower().strip()
    for pattern in DANGEROUS_PATTERNS:
        if pattern.lower() in cmd_lower:
            return True
    return False


def needs_confirmation(cmd: str) -> bool:
    """检查命令是否需要用户确认"""
    cmd_lower = cmd.lower().strip()
    for keyword in HIGH_RISK_KEYWORDS:
        if keyword.lower() in cmd_lower:
            return True
    return False


def ask_user_confirm(action_desc: str) -> bool:
    """请求用户确认高风险操作"""
    print(f"\n⚠️  高风险操作: {action_desc}")
    response = input("   确认执行？(y/N): ").strip().lower()
    return response in ("y", "yes", "是")
