#!/usr/bin/env python3
"""PreToolUse hook: block destructive Bash commands with exit code 2."""

from __future__ import annotations

import json
import re
import sys

BLOCKED = re.compile(
    r"\b(rm\s+-rf|rmdir\s+/s|del\s+/[fq]|format\s+|Remove-Item\s+.*-Recurse)\b",
    re.IGNORECASE,
)


def main() -> None:
    try:
        event = json.load(sys.stdin)
    except json.JSONDecodeError:
        sys.exit(0)

    if event.get("tool_name") != "Bash":
        sys.exit(0)

    command = event.get("tool_input", {}).get("command", "")
    if BLOCKED.search(command):
        print(
            json.dumps(
                {
                    "hookSpecificOutput": {
                        "hookEventName": "PreToolUse",
                        "permissionDecision": "deny",
                        "permissionDecisionReason": (
                            "Destructive command blocked by PreToolUse hook (exit 2)"
                        ),
                    }
                }
            )
        )
        sys.exit(2)

    sys.exit(0)


if __name__ == "__main__":
    main()
