"""Tests for project type detection."""

import json
from pathlib import Path

from tokenguard.detector import (
    detect_existing_hooks,
    detect_existing_mirror,
    detect_project_type,
    has_grip_hook,
)


def test_detect_python_pyproject(tmp_path):
    (tmp_path / "pyproject.toml").write_text("[project]\nname = 'test'\n")
    assert detect_project_type(tmp_path) == "python"


def test_detect_python_requirements(tmp_path):
    (tmp_path / "requirements.txt").write_text("flask\n")
    assert detect_project_type(tmp_path) == "python"


def test_detect_javascript(tmp_path):
    (tmp_path / "package.json").write_text('{"name": "test"}\n')
    assert detect_project_type(tmp_path) == "javascript"


def test_detect_rust(tmp_path):
    (tmp_path / "Cargo.toml").write_text("[package]\nname = 'test'\n")
    assert detect_project_type(tmp_path) == "rust"


def test_detect_go(tmp_path):
    (tmp_path / "go.mod").write_text("module test\n")
    assert detect_project_type(tmp_path) == "go"


def test_detect_generic_empty(tmp_path):
    assert detect_project_type(tmp_path) == "generic"


def test_detect_generic_makefile_only(tmp_path):
    (tmp_path / "Makefile").write_text("all:\n\techo hi\n")
    assert detect_project_type(tmp_path) == "generic"


def test_python_takes_priority_over_makefile(tmp_path):
    (tmp_path / "pyproject.toml").write_text("[project]\n")
    (tmp_path / "Makefile").write_text("all:\n")
    assert detect_project_type(tmp_path) == "python"


def test_detect_existing_mirror_not_present(tmp_path):
    assert detect_existing_mirror(tmp_path) is None


def test_detect_existing_mirror_present(tmp_path):
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir()
    data = {"version": "0.1.0", "project_type": "python"}
    (claude_dir / ".tokenguard.json").write_text(json.dumps(data))
    result = detect_existing_mirror(tmp_path)
    assert result is not None
    assert result["version"] == "0.1.0"


def test_detect_existing_mirror_corrupt_json(tmp_path):
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir()
    (claude_dir / ".tokenguard.json").write_text("not json")
    assert detect_existing_mirror(tmp_path) is None


# =========================================================================
# Hook detection (GRIP coexistence)
# =========================================================================

def test_detect_existing_hooks_empty(tmp_path):
    """No settings.json -> no hooks."""
    assert detect_existing_hooks(tmp_path) == []


def test_detect_existing_hooks_from_project(tmp_path):
    """Hooks in project settings.json should be detected."""
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir()
    settings = {
        "hooks": {
            "PreToolUse": [{
                "matcher": ".*",
                "hooks": [{"type": "command", "command": "python3 hooks/my-hook.py"}]
            }]
        }
    }
    (claude_dir / "settings.json").write_text(json.dumps(settings))
    hooks = detect_existing_hooks(tmp_path)
    assert len(hooks) == 1
    assert "my-hook.py" in hooks[0]


def test_has_grip_hook_false(tmp_path):
    """No GRIP hook -> False."""
    assert has_grip_hook(tmp_path) is False


def test_has_grip_hook_true(tmp_path):
    """GRIP context-gate-hook.py registered -> True."""
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir()
    settings = {
        "hooks": {
            "PreToolUse": [{
                "matcher": ".*",
                "hooks": [{
                    "type": "command",
                    "command": "python3 ~/.claude/hooks/context-gate-hook.py"
                }]
            }]
        }
    }
    (claude_dir / "settings.json").write_text(json.dumps(settings))
    assert has_grip_hook(tmp_path) is True


def test_has_grip_hook_not_triggered_by_mirror(tmp_path):
    """Mirror hook should not be detected as GRIP hook."""
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir()
    settings = {
        "hooks": {
            "PreToolUse": [{
                "matcher": ".*",
                "hooks": [{
                    "type": "command",
                    "command": "python3 .claude/hooks/mirror-hook.py"
                }]
            }]
        }
    }
    (claude_dir / "settings.json").write_text(json.dumps(settings))
    assert has_grip_hook(tmp_path) is False
