"""Tests for godotiq_dependency_graph tool."""

from __future__ import annotations

from godotiq.session import GodotIQSession
from godotiq.tools.code import _build_dependency_graph


class TestDependencyGraphBasic:
    def test_graph_basic(self, session: GodotIQSession) -> None:
        """Basic graph structure is correct."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/economy_manager.gd")
        assert result["file"] == "res://scripts/autoloads/economy_manager.gd"
        assert result["class_name"] == "EconomyManagerClass"
        assert result["is_autoload"] is True
        assert "emits_signals" in result
        assert "impact_rating" in result

    def test_graph_autoload_refs(self, session: GodotIQSession) -> None:
        """Autoload references are listed."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/progression_manager.gd")
        autoload_names = [r["autoload_name"] for r in result["references_autoloads"]]
        assert "GameManager" in autoload_names
        assert "EconomyManager" in autoload_names

    def test_graph_referenced_by(self, session: GodotIQSession) -> None:
        """Referenced-by shows scripts that depend on this file."""
        result = _build_dependency_graph(session, "res://scripts/entities/printer.gd")
        # printer.gd is the GameManager autoload script
        assert "referenced_by" in result

    def test_graph_signal_listeners(self, session: GodotIQSession) -> None:
        """Signal emissions are cross-referenced with listeners."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/economy_manager.gd")
        assert "emits_signals" in result
        signal_names = [s["signal_name"] for s in result["emits_signals"]]
        assert "cash_changed" in signal_names


class TestDependencyGraphDepth:
    def test_graph_depth_1(self, session: GodotIQSession) -> None:
        """Depth 1 shows only direct dependencies."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/progression_manager.gd", depth=1)
        assert "depth_trace" not in result

    def test_graph_depth_2(self, session: GodotIQSession) -> None:
        """Depth 2 includes depth trace with dependency details."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/progression_manager.gd", depth=2)
        assert "depth_trace" in result
        trace = result["depth_trace"]
        assert isinstance(trace, dict)
        # progression_manager references GameManager and EconomyManager autoloads
        # so depth trace should include their script paths
        for path, info in trace.items():
            assert "depends_on" in info
            assert "depended_by" in info


class TestDependencyGraphImpact:
    def test_graph_impact_rating_autoload(self, session: GodotIQSession) -> None:
        """Autoload scripts get higher impact rating."""
        result = _build_dependency_graph(session, "res://scripts/autoloads/economy_manager.gd")
        assert result["impact_rating"] in ("medium", "high", "critical")

    def test_graph_impact_rating_entity(self, session: GodotIQSession) -> None:
        """Isolated entity script gets low impact rating."""
        result = _build_dependency_graph(session, "res://scripts/entities/worker.gd")
        assert result["impact_rating"] == "low"

    def test_graph_missing_file(self, session: GodotIQSession) -> None:
        """Missing file returns error."""
        result = _build_dependency_graph(session, "res://nonexistent.gd")
        assert "error" in result
