"""Tests for detail level filtering across tools."""

from __future__ import annotations

from pathlib import Path

import pytest

from godotiq.session import GodotIQSession
from godotiq.tools.code import (
    _build_dependency_graph,
    _build_signal_map,
    _build_validate,
)
from godotiq.tools.memory import _build_file_context, _build_project_summary


@pytest.fixture
def session() -> GodotIQSession:
    root = Path(__file__).resolve().parent / "fixtures" / "sample_project"
    s = GodotIQSession(root)
    s.load()
    return s


class TestDependencyGraphDetail:
    def test_brief_has_fewer_keys(self, session):
        scripts = list(session._gd_scripts.keys())
        if not scripts:
            pytest.skip("No scripts in sample project")
        brief = _build_dependency_graph(session, scripts[0], detail="brief")
        normal = _build_dependency_graph(session, scripts[0], detail="normal")
        assert "impact_rating" in brief
        assert "emits_signals" in brief
        assert len(brief) < len(normal)

    def test_normal_is_default(self, session):
        scripts = list(session._gd_scripts.keys())
        if not scripts:
            pytest.skip("No scripts in sample project")
        default = _build_dependency_graph(session, scripts[0])
        normal = _build_dependency_graph(session, scripts[0], detail="normal")
        assert default.keys() == normal.keys()

    def test_full_includes_depth_trace(self, session):
        scripts = list(session._gd_scripts.keys())
        if not scripts:
            pytest.skip("No scripts in sample project")
        full = _build_dependency_graph(session, scripts[0], detail="full")
        assert "depth_trace" in full


class TestSignalMapDetail:
    def test_brief_only_orphans_and_missing(self, session):
        brief = _build_signal_map(session, detail="brief")
        assert "orphans" in brief
        assert "missing" in brief
        assert "signals" not in brief

    def test_normal_includes_signals(self, session):
        normal = _build_signal_map(session, detail="normal")
        # With find="all" (default), signals should be present
        assert "signals" in normal or "orphans" in normal


class TestValidateDetail:
    def test_brief_has_no_issues_list(self, session):
        brief = _build_validate(session, detail="brief")
        assert "by_severity" in brief
        assert "issues" not in brief

    def test_normal_has_issues_list(self, session):
        normal = _build_validate(session, detail="normal")
        assert "issues" in normal


class TestProjectSummaryDetail:
    def test_brief_has_counts(self, session):
        brief = _build_project_summary(session, detail="brief")
        assert "counts" in brief
        assert "architecture" not in brief

    def test_normal_has_architecture(self, session):
        normal = _build_project_summary(session, detail="normal")
        assert "architecture" in normal

    def test_full_has_scene_structure(self, session):
        full = _build_project_summary(session, detail="full")
        assert "scene_structure" in full


class TestFileContextDetail:
    def test_brief_gd_has_public_api(self, session):
        scripts = list(session._gd_scripts.keys())
        if not scripts:
            pytest.skip("No scripts in sample project")
        brief = _build_file_context(session, scripts[0], detail="brief")
        assert "public_api" in brief
        assert "depends_on" not in brief

    def test_normal_gd_has_depends_on(self, session):
        scripts = list(session._gd_scripts.keys())
        if not scripts:
            pytest.skip("No scripts in sample project")
        normal = _build_file_context(session, scripts[0], detail="normal")
        assert "depends_on" in normal
