"""Tests for Bug 2 fix: spatial_audit overlap excludes non-spatial nodes."""

from __future__ import annotations

from godotiq.parsers.tscn_parser import TscnNode
from godotiq.parsers.scene_resolver import WorldNode
from godotiq.tools.spatial import _check_overlapping


class TestOverlapFiltersSpatialTypes:
    """Only 3D-relevant nodes trigger overlap detection."""

    def test_non_spatial_instances_at_origin_no_overlap(self) -> None:
        """Control, Node, and CanvasLayer instances at (0,0,0) should not overlap."""
        control = WorldNode(
            node=TscnNode(name="HUD", type="Control"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="HUD",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/HUD.tscn",
        )
        handler = WorldNode(
            node=TscnNode(name="EventHandler", type="Node"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="EventHandler",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/EventHandler.tscn",
        )
        canvas = WorldNode(
            node=TscnNode(name="Overlay", type="CanvasLayer"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="Overlay",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/Overlay.tscn",
        )
        issues = _check_overlapping([control, handler, canvas])
        assert len(issues) == 0, "Non-spatial types must not trigger overlap"

    def test_spatial_instances_at_same_position_overlap(self) -> None:
        """Two Node3D instances at the same position SHOULD be flagged."""
        mesh_a = WorldNode(
            node=TscnNode(name="TreeA", type="Node3D"),
            world_position=(5.0, 0.0, 5.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="TreeA",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/Tree.tscn",
        )
        mesh_b = WorldNode(
            node=TscnNode(name="TreeB", type="Node3D"),
            world_position=(5.0, 0.0, 5.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="TreeB",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/Tree.tscn",
        )
        issues = _check_overlapping([mesh_a, mesh_b])
        assert len(issues) == 1
        assert issues[0]["node"] == "TreeA"
        assert issues[0]["other_node"] == "TreeB"

    def test_glb_instances_overlap(self) -> None:
        """GLB model instances (empty type) at same position → overlap."""
        model_a = WorldNode(
            node=TscnNode(name="RockA", type=""),
            world_position=(1.0, 0.0, 1.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="RockA",
            depth=1,
            is_instance_root=True,
            instance_source="res://assets/models/rock.glb",
        )
        model_b = WorldNode(
            node=TscnNode(name="RockB", type=""),
            world_position=(1.0, 0.0, 1.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="RockB",
            depth=1,
            is_instance_root=True,
            instance_source="res://assets/models/rock.glb",
        )
        issues = _check_overlapping([model_a, model_b])
        assert len(issues) == 1

    def test_mixed_spatial_and_nonspatial_no_false_positives(self) -> None:
        """Non-spatial at origin + spatial at origin: only spatial counted."""
        hud = WorldNode(
            node=TscnNode(name="HUD", type="Control"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="HUD",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/HUD.tscn",
        )
        mesh = WorldNode(
            node=TscnNode(name="Building", type="MeshInstance3D"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="Building",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/Building.tscn",
        )
        issues = _check_overlapping([hud, mesh])
        # Only one spatial node → no pair → no overlap
        assert len(issues) == 0

    def test_spatial_instances_at_different_positions_no_overlap(self) -> None:
        """Spatial nodes at different positions should NOT overlap."""
        a = WorldNode(
            node=TscnNode(name="HouseA", type="Node3D"),
            world_position=(0.0, 0.0, 0.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="HouseA",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/House.tscn",
        )
        b = WorldNode(
            node=TscnNode(name="HouseB", type="Node3D"),
            world_position=(10.0, 0.0, 10.0),
            world_rotation=(0.0, 0.0, 0.0),
            world_scale=(1.0, 1.0, 1.0),
            full_path="HouseB",
            depth=1,
            is_instance_root=True,
            instance_source="res://scenes/House.tscn",
        )
        issues = _check_overlapping([a, b])
        assert len(issues) == 0
