"""
🔴 RED TDD: DM Structural Guard Self-Sabotage Fix

Reproduces Bug 2: The intent 'tap direct message icon inbox' is NOT classified
as a nav intent, causing the Structural Guard to reject the correct VLM match
in the nav bar zone.

These tests MUST FAIL before the fix and PASS after.
"""

from GramAddict.core.telepathic_engine import TelepathicEngine


class TestNavIntentClassification:
    """Verifies that all navigation-related intents are correctly classified."""

    def test_dm_intent_is_classified_as_nav_intent(self):
        """
        The intent 'tap direct message icon inbox' MUST be treated as a nav intent
        so the structural guard allows clicking elements in the nav bar zone.
        """
        engine = TelepathicEngine()
        screen_height = 2400

        # DM icon is in the nav bar zone (top right, but the 'direct tab'
        # element is at the bottom nav bar on some Instagram layouts)
        dm_node = {
            "semantic_string": "description: 'Message', id context: 'direct tab'",
            "y": int(screen_height * 0.95),  # Bottom nav bar zone
            "area": 3000,
            "class_name": "android.widget.ImageView",
            "resource_id": "direct_tab",
        }

        intent = "tap direct message icon inbox"

        # The node should be viable — it's a nav intent targeting the nav bar
        is_valid = engine._structural_sanity_check(dm_node, intent, screen_height)

        assert is_valid is True, (
            "Structural Guard rejected 'direct tab' for DM intent. "
            "This is the exact bug: 'tap direct message icon inbox' is not classified as nav intent."
        )

    def test_inbox_intent_is_classified_as_nav_intent(self):
        """Variant: 'tap inbox' should also be treated as navigation."""
        engine = TelepathicEngine()
        screen_height = 2400

        inbox_node = {
            "semantic_string": "description: 'Inbox', id context: 'direct_inbox'",
            "y": int(screen_height * 0.95),
            "area": 2500,
            "class_name": "android.widget.ImageView",
            "resource_id": "direct_inbox",
        }

        intent = "tap inbox"

        is_valid = engine._structural_sanity_check(inbox_node, intent, screen_height)
        assert is_valid is True, "Structural Guard rejected inbox node for 'tap inbox' intent."

    def test_notification_intent_is_classified_as_nav_intent(self):
        """'tap heart icon notifications' should also be treated as navigation."""
        engine = TelepathicEngine()
        screen_height = 2400

        notification_node = {
            "semantic_string": "description: 'Activity', id context: 'notification_tab'",
            "y": int(screen_height * 0.95),
            "area": 2500,
            "class_name": "android.widget.ImageView",
            "resource_id": "notification_tab",
        }

        intent = "tap heart icon notifications"

        is_valid = engine._structural_sanity_check(notification_node, intent, screen_height)
        assert is_valid is True, "Structural Guard rejected notification node for heart icon intent."

    def test_regular_post_intent_still_blocked_in_nav_zone(self):
        """
        Non-nav intents (like 'tap like button') targeting elements in the nav bar
        zone must STILL be rejected. We're not weakening the guard.
        """
        engine = TelepathicEngine()
        screen_height = 2400

        misplaced_like_node = {
            "semantic_string": "description: 'Like', id context: 'some_like_button'",
            "y": int(screen_height * 0.95),
            "area": 2000,
            "class_name": "android.widget.ImageView",
        }

        intent = "tap like button"

        is_valid = engine._structural_sanity_check(misplaced_like_node, intent, screen_height)
        assert is_valid is False, (
            "Structural Guard allowed a like button in the nav bar zone. " "Non-nav intents should still be blocked."
        )
