"""
E2E: Profile Workflows
========================
Tests the FULL production pipeline for profile screens:
- Own profile
- Other user profile
- Followers/Following lists

Mock: ONLY the device (XML dumps).
Real: Cognitive stack, PluginRegistry, SessionState, Config.
"""

import os

FIXTURES_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "fixtures")


def _load_fixture(name):
    with open(os.path.join(FIXTURES_DIR, name), "r", encoding="utf-8") as f:
        return f.read()


def test_user_profile_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    A user profile page must be processable without crashes.
    """
    profile_xml = _load_fixture("user_profile_dump.xml")
    device = make_real_device_with_xml([profile_xml, profile_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on user profile."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the Profile!"

    assert "back" not in device.pressed_keys, "obstacle_guard false positive on user profile!"


def test_scraping_profile_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    The scraping profile dump must be processable without crashes.
    """
    scrape_xml = _load_fixture("scraping_profile_dump.xml")
    device = make_real_device_with_xml([scrape_xml, scrape_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on scraping profile."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the scraping profile!"


def test_followers_list_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    The followers list must be processable without crashes.
    """
    followers_xml = _load_fixture("followers_list_dump.xml")
    device = make_real_device_with_xml([followers_xml, followers_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on followers list."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the followers list!"


def test_unfollow_list_processes_without_crashes(make_real_device_with_xml, e2e_workflow_ctx):
    """
    The unfollow list must be processable without crashes.
    """
    unfollow_xml = _load_fixture("unfollow_list_dump.xml")
    device = make_real_device_with_xml([unfollow_xml, unfollow_xml])
    results, ctx = e2e_workflow_ctx(device)

    executed_count = sum(1 for r in results if r.executed)
    assert executed_count >= 1, f"Only {executed_count} plugin(s) executed on unfollow list."
    # 🚨 BEHAVIORAL TRUTH ASSERTION 🚨
    assert (
        len(device.clicks) > 0 or len(device.swipes) > 0
    ), "LIE DETECTED: The pipeline claimed success but did not interact with the unfollow list!"
