"""Tests for Bug 3 fix: RESET animations not flagged as orphan."""

from __future__ import annotations

from pathlib import Path

from godotiq.session import GodotIQSession
from godotiq.tools.animation import _build_animation_audit, _is_reset_animation


class TestResetAnimationFilter:
    """RESET animations should never be reported as orphan_animation."""

    def test_reset_by_name(self) -> None:
        """Animation named 'RESET' is always a reset animation."""
        assert _is_reset_animation({"name": "RESET", "length": 0.001}) is True

    def test_reset_by_name_even_long(self) -> None:
        """Even a long animation named RESET is recognized (editor quirk)."""
        assert _is_reset_animation({"name": "RESET", "length": 5.0}) is True

    def test_reset_by_short_duration(self) -> None:
        """Any animation <=0.01s is a reset animation."""
        assert _is_reset_animation({"name": "Animation_ud13x", "length": 0.001}) is True
        assert _is_reset_animation({"name": "Animation_ld0tj", "length": 0.01}) is True

    def test_normal_animation_not_reset(self) -> None:
        """Regular animations are NOT reset animations."""
        assert _is_reset_animation({"name": "walk", "length": 1.0}) is False
        assert _is_reset_animation({"name": "idle_ground", "length": 1.5}) is False

    def test_reset_not_in_orphan_results(self) -> None:
        """RESET animation in fixture scene is not reported as orphan."""
        reset_root = Path(__file__).resolve().parent.parent / "fixtures" / "reset_anim_project"
        session = GodotIQSession(reset_root)
        session.load()

        result = _build_animation_audit(
            session, scope="scene", scene_filter="res://scenes/reset_anim_scene.tscn"
        )
        orphans = [i for i in result["issues"] if i["type"] == "orphan_animation"]
        orphan_names = {i["animation"] for i in orphans}

        # RESET should NOT be orphaned
        assert "RESET" not in orphan_names, "RESET must not be flagged as orphan"

        # unused_anim SHOULD still be orphaned
        assert "unused_anim" in orphan_names, "unused_anim should be flagged as orphan"

    def test_normal_orphan_still_detected(self) -> None:
        """Non-reset unreferenced animations are still flagged as orphan."""
        anim_root = Path(__file__).resolve().parent.parent / "fixtures" / "anim_project"
        session = GodotIQSession(anim_root)
        session.load()

        result = _build_animation_audit(
            session, scope="scene", scene_filter="res://scenes/animated_character.tscn"
        )
        orphans = [i for i in result["issues"] if i["type"] == "orphan_animation"]
        orphan_names = {i["animation"] for i in orphans}
        assert "idle_ground" in orphan_names, "idle_ground should still be orphan"
