#!/usr/bin/env python3
"""Hook: PostToolUse (Edit|Write)
Auto-copy modified skills to the test project after edit.
Only triggers when the edited file is in .claude/commands/*.md
Cross-platform: works on macOS, Linux, and Windows.
"""

import json
import os
import shutil
import sys
from pathlib import Path


def main():
    data = json.load(sys.stdin)
    tool_input = data.get("tool_input", {})
    file_path = tool_input.get("file_path", "")

    if not file_path:
        sys.exit(0)

    path = Path(file_path)

    # Check if the file is a skill (in .claude/commands/)
    if "commands" not in path.parts or path.suffix != ".md":
        sys.exit(0)

    # Target project — configurable via env var
    default_target = Path.home() / "Desktop" / "TestGodot" / ".claude" / "commands"
    target_dir = Path(os.environ.get("GOLEM_SKILLS_TARGET", str(default_target)))

    if not target_dir.is_dir():
        sys.exit(0)

    # Copy the modified file
    dest = target_dir / path.name
    shutil.copy2(path, dest)

    output = {
        "suppressOutput": False,
        "systemMessage": f"✓ Skill {path.name} copied to test project",
    }
    json.dump(output, sys.stdout)


if __name__ == "__main__":
    main()
