import os
import sys

import pytest

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))

from GramAddict.core.telepathic_engine import TelepathicEngine


@pytest.fixture
def engine():
    # Instantiate directly to avoid singleton contamination from mocks
    return TelepathicEngine()


def test_keyword_nav_threshold(engine):
    """
    TDD Case: "tap messages tab" should NOT match "Reels" (clips_tab).
    Intent words: {"messages", "tab"}
    Reels node description: "Reels", resource_id: "clips_tab"
    Matches "tab" -> score 0.5.
    Current threshold 0.45 -> matches (WRONG).
    New threshold for nav intents should be 1.0.
    """
    reels_node = {
        "x": 500,
        "y": 2000,
        "area": 100,
        "semantic_string": "description: 'Reels', id context: 'clips tab'",
        "resource_id": "com.instagram.android:id/clips_tab",
        "original_attribs": {"desc": "Reels", "text": "", "resource-id": "com.instagram.android:id/clips_tab"},
    }

    # Intent: "tap messages tab"
    # Result should be None because "messages" is missing.
    res = engine._keyword_match_score("tap messages tab", [reels_node])
    assert res is None


def test_direct_tab_fast_path(engine):
    """
    Verify that _core_navigation_fast_path returns None without Qdrant data
    (Blank Start architecture). Navigation discovery is Qdrant-only.
    The keyword_match_score fallback handles it with resource-id matching.
    """
    direct_node = {
        "x": 800,
        "y": 2300,
        "area": 100,
        "semantic_string": "Direct",
        "resource_id": "com.instagram.android:id/direct_tab",
        "original_attribs": {"resource-id": "com.instagram.android:id/direct_tab"},
    }

    # Without Qdrant data, fast path returns None (Blank Start)
    res = engine._core_navigation_fast_path("tap messages tab", [direct_node])

    # In Blank Start, if Qdrant has no learned data, this MUST return None
    # to force the agent into telepathic discovery mode
    assert res is None, "Fast path should return None without learned Qdrant data"
