"""Tests for the scaffolding engine."""

import json
from pathlib import Path

from tokenguard.scaffold import (
    generate_claude_md,
    generate_mirror_json,
    generate_settings_json,
    scaffold_project,
)


def test_generate_claude_md_standalone():
    content = generate_claude_md("python", grip_global=False, project_name="myapp")
    assert "myapp" in content
    assert "python" in content.lower()
    assert "Safety Gates" in content
    assert "Dependency Guardian" in content


def test_generate_claude_md_grip_user():
    content = generate_claude_md("python", grip_global=True, project_name="myapp")
    assert "myapp" in content
    # Should NOT duplicate rules when GRIP is global
    assert "Global rules inherited" in content


def test_generate_settings_json():
    content = generate_settings_json(".claude/hooks/mirror-hook.py")
    settings = json.loads(content)
    assert "hooks" in settings
    assert "PreToolUse" in settings["hooks"]
    hook_cmd = settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"]
    assert "mirror-hook.py" in hook_cmd


def test_generate_mirror_json():
    content = generate_mirror_json(
        "python", False, {"a.md": "abc123"}, ["claude-md", "rules"]
    )
    data = json.loads(content)
    assert data["version"] == "0.2.0"
    assert data["project_type"] == "python"
    assert data["grip_global"] is False
    assert "a.md" in data["file_hashes"]


def test_scaffold_standalone_dry_run(tmp_path):
    (tmp_path / "pyproject.toml").write_text("[project]\n")
    result = scaffold_project(tmp_path, "python", standalone=True, dry_run=True)

    assert result["mode"] == "standalone"
    assert result["project_type"] == "python"
    files = result["files"]

    # Should have CLAUDE.md, memory, rules, hooks, settings, mirror.json
    assert ".claude/CLAUDE.md" in files
    assert ".claude/memory/MEMORY.md" in files
    assert ".claude/hooks/mirror-hook.py" in files
    assert ".claude/settings.json" in files
    assert ".claude/.tokenguard.json" in files

    # Rules should be present in standalone mode
    assert ".claude/rules/efficiency-rules.md" in files
    assert ".claude/rules/bash-safety.md" in files
    assert ".claude/rules/commit-standards.md" in files

    # Dry run should NOT write files
    assert not (tmp_path / ".claude" / "CLAUDE.md").exists()


def test_scaffold_standalone_writes_files(tmp_path):
    result = scaffold_project(tmp_path, "python", standalone=True)

    assert (tmp_path / ".claude" / "CLAUDE.md").exists()
    assert (tmp_path / ".claude" / "memory" / "MEMORY.md").exists()
    assert (tmp_path / ".claude" / "hooks" / "mirror-hook.py").exists()
    assert (tmp_path / ".claude" / "settings.json").exists()
    assert (tmp_path / ".claude" / ".tokenguard.json").exists()
    assert (tmp_path / ".claude" / "rules" / "efficiency-rules.md").exists()

    # Verify CLAUDE.md content
    claude_md = (tmp_path / ".claude" / "CLAUDE.md").read_text()
    assert "Safety Gates" in claude_md
    assert "python" in claude_md.lower()


def test_scaffold_no_hooks(tmp_path):
    result = scaffold_project(tmp_path, "python", standalone=True, no_hooks=True)

    assert ".claude/hooks/mirror-hook.py" not in result["files"]
    assert ".claude/settings.json" not in result["files"]
    assert not (tmp_path / ".claude" / "hooks").exists()


def test_scaffold_mirror_json_integrity(tmp_path):
    scaffold_project(tmp_path, "python", standalone=True)

    mirror = json.loads((tmp_path / ".claude" / ".tokenguard.json").read_text())
    assert mirror["version"] == "0.2.0"
    assert mirror["project_type"] == "python"
    assert "claude-md" in mirror["installed_components"]
    assert "rules" in mirror["installed_components"]
    assert "hooks" in mirror["installed_components"]
    # All tracked files should have hashes
    for rel_path, hash_val in mirror["file_hashes"].items():
        assert len(hash_val) == 16  # SHA-256 truncated to 16 chars


def test_scaffold_javascript(tmp_path):
    result = scaffold_project(tmp_path, "javascript", standalone=True)
    claude_md = (tmp_path / ".claude" / "CLAUDE.md").read_text()
    assert "JavaScript" in claude_md or "javascript" in claude_md.lower()


def test_scaffold_generic(tmp_path):
    result = scaffold_project(tmp_path, "generic", standalone=True)
    claude_md = (tmp_path / ".claude" / "CLAUDE.md").read_text()
    assert "generic" in claude_md.lower()
