"""Integration tests for the TSCN parser pipeline."""

from pathlib import Path

import pytest

from godotiq.parsers.tscn_parser import TscnScene, parse_tscn

FIXTURES = Path(__file__).resolve().parent.parent / "fixtures"


class TestParseAllFixtures:
    """Parse every fixture file and verify no exceptions are raised."""

    @pytest.mark.parametrize("filename", [
        "worker.tscn",
        "fdm_printer.tscn",
        "furniture_slot_placeholder.tscn",
        "main.tscn",
    ])
    def test_parse_fixture_no_exceptions(self, filename: str) -> None:
        path = FIXTURES / filename
        scene = parse_tscn(str(path))
        assert isinstance(scene, TscnScene)
        assert scene.root is not None
        assert len(scene.nodes) > 0

    @pytest.mark.parametrize("filename", [
        "worker.tscn",
        "fdm_printer.tscn",
        "furniture_slot_placeholder.tscn",
        "main.tscn",
    ])
    def test_all_nodes_have_non_empty_name(self, filename: str) -> None:
        scene = parse_tscn(str(FIXTURES / filename))
        for node in scene.nodes:
            assert node.name, f"Node with empty name in {filename}"


class TestFindNodeByPath:
    """Test find_node query method."""

    def test_find_node_multi_level_path(self) -> None:
        scene = parse_tscn(str(FIXTURES / "main.tscn"))
        node = scene.find_node("Building/LockedZones/PostProcessingZone")
        assert node is not None
        assert node.name == "PostProcessingZone"


class TestGetAllScripts:
    """Test get_all_scripts query method."""

    def test_scripts_are_valid_res_paths(self) -> None:
        scene = parse_tscn(str(FIXTURES / "main.tscn"))
        scripts = scene.get_all_scripts()
        assert len(scripts) > 0
        for path in scripts:
            assert path.startswith("res://"), f"Invalid script path: {path}"


class TestGetAllInstances:
    """Test get_all_instances query method."""

    def test_instances_are_valid_res_paths(self) -> None:
        scene = parse_tscn(str(FIXTURES / "main.tscn"))
        instances = scene.get_all_instances()
        assert len(instances) > 0
        for path in instances:
            assert path.startswith("res://"), f"Invalid instance path: {path}"


class TestGetNodesByGroup:
    """Test get_nodes_by_group query method."""

    def test_group_results_have_correct_group(self) -> None:
        scene = parse_tscn(str(FIXTURES / "fdm_printer.tscn"))
        nodes = scene.get_nodes_by_group("interactable")
        assert len(nodes) > 0
        for node in nodes:
            assert "interactable" in node.groups

    def test_printer_slots_count(self) -> None:
        scene = parse_tscn(str(FIXTURES / "main.tscn"))
        nodes = scene.get_nodes_by_group("printer_slots")
        assert len(nodes) == 6


class TestGetNodesByType:
    """Test get_nodes_by_type query method."""

    def test_type_results_have_correct_type(self) -> None:
        scene = parse_tscn(str(FIXTURES / "main.tscn"))
        nodes = scene.get_nodes_by_type("Marker3D")
        assert len(nodes) == 35
        for node in nodes:
            assert node.type == "Marker3D"
