"""
E2E Test: Home Feed Navigation
Validates that the IntentResolver and TelepathicEngine can correctly navigate
the home feed using a REAL XML dump, without relying on legacy mocks.
"""

import pytest

from GramAddict.core.perception.intent_resolver import IntentResolver
from GramAddict.core.perception.spatial_parser import SpatialParser


def _load_home_feed_xml():
    with open("tests/fixtures/home_feed_with_ad.xml", "r", encoding="utf-8") as f:
        return f.read()


@pytest.mark.live_llm
def test_home_feed_like_button_extraction(make_real_device_with_image):
    """
    Tests if the VLM can find the like button on a real home feed dump.
    """
    xml = _load_home_feed_xml()
    parser = SpatialParser()
    root = parser.parse(xml)
    candidates = parser.get_clickable_nodes(root)

    device = make_real_device_with_image("tests/fixtures/home_feed_with_ad.jpg")
    resolver = IntentResolver()

    result = resolver.resolve("tap like button", candidates, device)

    assert result is not None, "Visual discovery returned None for 'tap like button' on Home Feed"

    def _node_has_marker(node, marker: str) -> bool:
        rid = (node.resource_id or "").lower()
        desc = (node.content_desc or "").lower()
        if marker in rid or marker in desc:
            return True
        for child in node.children:
            if _node_has_marker(child, marker):
                return True
        return False

    assert _node_has_marker(result, "like_button") or _node_has_marker(result, "like"), (
        f"VLM picked WRONG element for 'tap like button'!\n"
        f"  Selected: id='{result.resource_id}', desc='{result.content_desc}', "
        f"text='{result.text}'"
    )


@pytest.mark.live_llm
def test_home_feed_post_author_extraction(make_real_device_with_image):
    """
    Tests if the VLM can identify the post author's header/username.
    """
    xml = _load_home_feed_xml()
    parser = SpatialParser()
    root = parser.parse(xml)
    candidates = parser.get_clickable_nodes(root)

    device = make_real_device_with_image("tests/fixtures/home_feed_with_ad.jpg")
    resolver = IntentResolver()

    result = resolver.resolve("tap 'Profile picture' of the author", candidates, device)

    assert result is not None, "Visual discovery returned None for 'tap Profile picture of the author'"

    rid = (result.resource_id or "").lower()
    desc = (result.content_desc or "").lower()
    text = (result.text or "").lower()

    is_author = "row_feed_photo_profile_name" in rid or "millionlords" in desc or "millionlords" in text
    assert is_author, f"VLM picked wrong element! Selected id='{rid}', desc='{desc}', text='{text}'"


@pytest.mark.live_llm
def test_home_feed_comment_button_extraction(make_real_device_with_image):
    """
    Tests if the VLM can find the comment button to open the comment sheet.
    """
    xml = _load_home_feed_xml()
    parser = SpatialParser()
    root = parser.parse(xml)
    candidates = parser.get_clickable_nodes(root)

    device = make_real_device_with_image("tests/fixtures/home_feed_with_ad.jpg")
    resolver = IntentResolver()

    result = resolver.resolve("tap 'comment' button", candidates, device)

    assert result is not None, "Visual discovery returned None for 'tap comment button'"

    def _node_has_marker(node, marker: str) -> bool:
        rid = (node.resource_id or "").lower()
        desc = (node.content_desc or "").lower()
        if marker in rid or marker in desc:
            return True
        for child in node.children:
            if _node_has_marker(child, marker):
                return True
        return False

    assert _node_has_marker(result, "comment"), (
        f"VLM picked WRONG element for 'tap comment button'!\n"
        f"  Selected: id='{result.resource_id}', desc='{result.content_desc}'"
    )
