"""E2E regression test to ensure multiline text editor feature is completely removed.

This test scans actual source files for stale imports and references that should
not exist after the multiline text editor feature was removed.
"""

import pytest
from pathlib import Path


def read_file_lines(filepath: Path) -> list[str]:
    """Read file and return lines with line numbers."""
    with open(filepath, 'r', encoding='utf-8') as f:
        return f.readlines()


def test_no_has_text_editor_import_in_text_editor_ops():
    """Verify has_text_editor is not imported in text_editor_ops.py"""
    file_path = Path('src/operators/text_editor_ops.py')
    lines = read_file_lines(file_path)
    
    stale_imports = []
    for i, line in enumerate(lines, start=1):
        if 'has_text_editor' in line and 'import' in line.lower():
            stale_imports.append((i, line.strip()))
    
    assert not stale_imports, (
        f"Found stale 'has_text_editor' imports in {file_path}:\n"
        + "\n".join(f"  Line {num}: {content}" for num, content in stale_imports)
    )


def test_no_has_text_editor_import_in_panels():
    """Verify has_text_editor is not imported in panels.py"""
    file_path = Path('src/ui/panels.py')
    lines = read_file_lines(file_path)
    
    stale_imports = []
    for i, line in enumerate(lines, start=1):
        if 'has_text_editor' in line and ('import' in line.lower() or 'from' in line.lower()):
            stale_imports.append((i, line.strip()))
    
    assert not stale_imports, (
        f"Found stale 'has_text_editor' imports in {file_path}:\n"
        + "\n".join(f"  Line {num}: {content}" for num, content in stale_imports)
    )


def test_no_multiline_text_operator_references_in_init():
    """Verify TEXT_TEXTURE_OT_edit_multiline_text is not referenced in __init__.py"""
    file_path = Path('src/__init__.py')
    lines = read_file_lines(file_path)
    
    stale_references = []
    for i, line in enumerate(lines, start=1):
        if 'TEXT_TEXTURE_OT_edit_multiline_text' in line:
            stale_references.append((i, line.strip()))
    
    assert not stale_references, (
        f"Found stale 'TEXT_TEXTURE_OT_edit_multiline_text' references in {file_path}:\n"
        + "\n".join(f"  Line {num}: {content}" for num, content in stale_references)
    )


def test_no_multiline_text_in_constants():
    """Verify multiline_text feature is completely removed from constants.py"""
    file_path = Path('src/utils/constants.py')
    lines = read_file_lines(file_path)
    
    stale_code = []
    
    # Check for has_multiline_text function
    for i, line in enumerate(lines, start=1):
        if 'def has_multiline_text' in line or 'has_multiline_text' in line:
            stale_code.append((i, line.strip()))
    
    # Check for "multiline_text" in ENABLED_FEATURES
    content = ''.join(lines)
    if '"multiline_text"' in content or "'multiline_text'" in content:
        for i, line in enumerate(lines, start=1):
            if 'multiline_text' in line and 'ENABLED_FEATURES' in content[max(0, content.find(line)-500):content.find(line)+500]:
                stale_code.append((i, line.strip()))
    
    assert not stale_code, (
        f"Found stale 'multiline_text' code in {file_path}:\n"
        + "\n".join(f"  Line {num}: {content}" for num, content in stale_code)
    )


def test_no_has_text_editor_function_in_constants():
    """Verify has_text_editor function doesn't exist in constants.py"""
    file_path = Path('src/utils/constants.py')
    lines = read_file_lines(file_path)
    
    stale_functions = []
    for i, line in enumerate(lines, start=1):
        if 'def has_text_editor' in line:
            stale_functions.append((i, line.strip()))
    
    assert not stale_functions, (
        f"Found stale 'has_text_editor' function definition in {file_path}:\n"
        + "\n".join(f"  Line {num}: {content}" for num, content in stale_functions)
    )