#!/usr/bin/env python3
"""PreToolUse hook: 保护 demo-miniprogram 目录不被删除或移入 _DEL/"""
import json, re, sys

try:
    data = json.load(sys.stdin)
except Exception:
    sys.exit(0)

tool = data.get("tool_name", "")
tool_input = data.get("tool_input") or {}

DEMO_DIR = "demo-miniprogram"

if tool == "Bash":
    cmd = tool_input.get("command", "")
    # 检查命令是否在对 demo-miniprogram 执行删除/移动操作
    if DEMO_DIR in cmd and re.search(r"\b(rm|rmdir|del|move|mv)\b", cmd, re.IGNORECASE):
        # 允许 mv 到非 _DEL 目录（如正常重命名），但阻止移入 _DEL
        if re.search(r"\brm\b|\brmdir\b|\bdel\b", cmd, re.IGNORECASE) or "_DEL" in cmd:
            print(json.dumps({
                "decision": "block",
                "reason": f"[demo-protect] apps/{DEMO_DIR}/ 禁止删除或移入 _DEL/。该目录是 UI 样式标杆校对基准。"
            }))
