"""
Shared fixtures and utilities for chaos engineering tests.
"""

import pytest


def generate_corrupted_xml(corruption_type: str) -> str:
    """
    Generates intentionally corrupted XML to stress-test parsers.
    Each corruption type simulates a real-world failure mode.
    """
    base_valid = (
        '<hierarchy rotation="0">'
        '<node index="0" text="" resource-id="com.instagram.android:id/action_bar_root" '
        'class="android.widget.FrameLayout" package="com.instagram.android" '
        'content-desc="" checkable="false" checked="false" clickable="false" '
        'enabled="true" focusable="false" focused="false" scrollable="false" '
        'long-clickable="false" password="false" selected="false" '
        'bounds="[0,0][1080,2400]">'
        '<node index="0" text="Like" resource-id="com.instagram.android:id/row_feed_button_like" '
        'class="android.widget.ImageView" package="com.instagram.android" '
        'content-desc="Like" checkable="false" checked="false" clickable="true" '
        'enabled="true" focusable="true" focused="false" scrollable="false" '
        'long-clickable="false" password="false" selected="false" '
        'bounds="[50,500][150,600]" />'
        "</node>"
        "</hierarchy>"
    )

    generators = {
        "EMPTY_STRING": lambda: "",
        "NONE_VALUE": lambda: None,
        "TRUNCATED_MID_TAG": lambda: base_valid[: len(base_valid) // 2],
        "UNICODE_INJECTION": lambda: base_valid.replace('text="Like"', 'text="L̵̡̧̢̛̛̛̘̗̣̥̱̲̲̝̪̣̗̝̠̫̲̤̱̪̞̻̙̜̺̩̰̫̝̥̩̭̩̫̦̠̦̣̣̬̤̤̠̗̣̲̬̟̣̰̝̥̤̜̻̫̙̥̘̻̝̯̗̼̣̮̲̻̝̹̩̗̥̖̝̝̪̣̜̜̱̣̱̻̮̬̮̬̗̖̟̩̭̜̀̀̈̀̀̀̑́̀̀̆̈́̐̑̈̈́̈́̉̿̈̉̆̂̃̉̆̉̑̉̈̊̏̀̒̌̽̈́̃̓̏̏͋̾̈́́̄̊̈́̽̅̒̓̈̈́̆̈̐̓̋̏̃͑̋̊̅̿̌̇̎̀̀̀̕̕̕͘̕̕̕̕̕͘͜͝͝i̷ke"'),
        "MASSIVE_DOM_10K_NODES": lambda: _generate_massive_dom(10000),
        "ZERO_SIZE_BOUNDS": lambda: base_valid.replace('bounds="[50,500][150,600]"', 'bounds="[500,500][500,500]"'),
        "NEGATIVE_COORDINATES": lambda: base_valid.replace(
            'bounds="[50,500][150,600]"', 'bounds="[-100,-200][50,100]"'
        ),
        "MISSING_CLOSING_TAGS": lambda: (
            '<hierarchy rotation="0">'
            '<node text="Like" clickable="true" bounds="[50,500][150,600]">'
            '<node text="nested">'
            # Intentionally missing closing tags
        ),
        "RECURSIVE_NESTING_500_DEEP": lambda: _generate_deep_nesting(500),
        "NULL_BYTES": lambda: base_valid.replace("Like", "Li\x00ke\x00"),
        "MALFORMED_BOUNDS": lambda: base_valid.replace('bounds="[50,500][150,600]"', 'bounds="NOT_A_BOUND"'),
        "ONLY_WHITESPACE": lambda: "   \n\t\n   ",
        "HTML_NOT_XML": lambda: "<html><body><div>Not XML at all</div></body></html>",
        "BINARY_GARBAGE": lambda: bytes(range(256)).decode("latin-1"),
        "EXTREMELY_LONG_TEXT": lambda: base_valid.replace('text="Like"', f'text="{"A" * 100000}"'),
    }

    generator = generators.get(corruption_type)
    if generator is None:
        raise ValueError(f"Unknown corruption type: {corruption_type}")
    return generator()


def _generate_massive_dom(count: int) -> str:
    """Generates a valid XML with 'count' nodes to test performance bounds."""
    parts = ['<hierarchy rotation="0">']
    for i in range(count):
        parts.append(
            f'<node index="{i}" text="item_{i}" '
            f'resource-id="com.instagram.android:id/item_{i}" '
            f'class="android.widget.TextView" '
            f'package="com.instagram.android" '
            f'clickable="true" bounds="[0,{i}][100,{i+50}]" />'
        )
    parts.append("</hierarchy>")
    return "".join(parts)


def _generate_deep_nesting(depth: int) -> str:
    """Generates deeply nested XML to test recursion limits."""
    xml = '<hierarchy rotation="0">'
    for i in range(depth):
        xml += f'<node index="{i}" text="level_{i}" class="android.widget.FrameLayout" '
        xml += 'package="com.instagram.android" bounds="[0,0][1080,2400]">'
    # Close all tags
    for _ in range(depth):
        xml += "</node>"
    xml += "</hierarchy>"
    return xml


# Valid XML fixtures for property tests
VALID_FEED_XML = (
    '<hierarchy rotation="0">'
    '<node index="0" text="" resource-id="com.instagram.android:id/action_bar_root" '
    'class="android.widget.FrameLayout" package="com.instagram.android" '
    'content-desc="" clickable="false" bounds="[0,0][1080,2400]">'
    '<node index="0" text="" resource-id="com.instagram.android:id/row_feed_button_like" '
    'class="android.widget.ImageView" package="com.instagram.android" '
    'content-desc="Like" clickable="true" bounds="[50,500][150,600]" />'
    '<node index="1" text="" resource-id="com.instagram.android:id/row_feed_button_comment" '
    'class="android.widget.ImageView" package="com.instagram.android" '
    'content-desc="Comment" clickable="true" bounds="[200,500][300,600]" />'
    '<node index="2" text="Follow" resource-id="com.instagram.android:id/profile_header_follow_button" '
    'class="android.widget.Button" package="com.instagram.android" '
    'content-desc="Follow" clickable="true" bounds="[800,300][1000,380]" />'
    '<node index="3" text="" resource-id="com.instagram.android:id/feed_tab" '
    'class="android.widget.ImageView" package="com.instagram.android" '
    'content-desc="Home" clickable="true" selected="true" bounds="[0,2300][216,2400]" />'
    '<node index="4" text="" resource-id="com.instagram.android:id/search_tab" '
    'class="android.widget.ImageView" package="com.instagram.android" '
    'content-desc="Search and explore" clickable="true" bounds="[216,2300][432,2400]" />'
    "</node>"
    "</hierarchy>"
)
