#!/usr/bin/env python3
"""Test MaxCpp Phase 32: C++ Text Processing Implementation."""

import time
from cutysoup import CutySoup
from bs4 import BeautifulSoup

# Test HTML with complex text structure
test_html = """
<html>
<head>
    <title>MaxCpp Text Processing Test</title>
</head>
<body>
    <div class="container">
        <h1>  Header with spaces  </h1>
        <p>First paragraph with <strong>bold text</strong> and content.</p>
        <div>
            <span>Nested span 1</span>
            <span>  Nested span 2 with spaces  </span>
            <span></span>
            <span>Nested span 3</span>
        </div>
        <p>Second paragraph with multiple    spaces    between words.</p>
    </div>
    <footer>
        <p>Footer paragraph</p>
    </footer>
</body>
</html>
"""

def test_cpp_text_methods():
    """Test the new C++ text processing methods."""
    print("MaxCpp Phase 32: C++ Text Processing Tests")
    print("=" * 50)
    
    soup = CutySoup(test_html)
    
    # Test direct access to C++ methods
    root = soup._tree.get_root()
    
    print("🔍 Testing Direct C++ Methods:")
    
    # Test basic get_text
    print("\n1. Basic get_text():")
    text = root.get_text()
    print(f"   Length: {len(text)} characters")
    print(f"   Preview: {text[:100]}...")
    
    # Test get_text_with_separator
    print("\n2. get_text_with_separator('|'):")
    text_sep = root.get_text_with_separator("|")
    print(f"   With separator: {text_sep[:100]}...")
    
    # Test get_text_stripped
    print("\n3. get_text_stripped():")
    text_stripped = root.get_text_stripped()
    print(f"   Stripped: {text_stripped[:100]}...")
    
    # Test get_strings
    print("\n4. get_strings() (C++ bulk operation):")
    strings = root.get_strings()
    print(f"   Total strings: {len(strings)}")
    print(f"   First 5: {strings[:5]}")
    
    # Test get_stripped_strings
    print("\n5. get_stripped_strings() (C++ bulk operation):")
    stripped_strings = root.get_stripped_strings()
    print(f"   Non-empty strings: {len(stripped_strings)}")
    print(f"   First 5: {stripped_strings[:5]}")

def test_python_api_integration():
    """Test that Python API correctly uses C++ methods."""
    print("\n🔍 Testing Python API Integration:")
    
    soup = CutySoup(test_html)
    div = soup.find('div', class_='container')
    
    # Test get_text with different parameters
    print("\n1. get_text() variations:")
    print(f"   Basic: {div.get_text()[:50]}...")
    print(f"   With separator: {div.get_text(separator='|')[:50]}...")
    print(f"   Stripped: {div.get_text(strip=True)[:50]}...")
    print(f"   Sep + Strip: {div.get_text(separator='|', strip=True)[:50]}...")
    
    # Test strings property (now uses C++ bulk operation)
    print("\n2. strings property (C++ bulk):")
    strings_list = list(div.strings)
    print(f"   Total strings: {len(strings_list)}")
    print(f"   First 3: {strings_list[:3]}")
    
    # Test stripped_strings property (now uses C++ bulk operation)
    print("\n3. stripped_strings property (C++ bulk):")
    stripped_list = list(div.stripped_strings)
    print(f"   Non-empty strings: {len(stripped_list)}")
    print(f"   First 3: {stripped_list[:3]}")

def benchmark_text_processing():
    """Benchmark C++ vs Python text processing."""
    print("\n🚀 Performance Benchmark: C++ vs BeautifulSoup4")
    print("=" * 50)
    
    # Create larger document
    large_html = "<html><body>"
    for i in range(100):
        large_html += f"""
        <div class="section-{i}">
            <h2>Section {i} Title</h2>
            <p>Paragraph 1 in section {i} with <strong>bold</strong> content.</p>
            <p>Paragraph 2 in section {i} with <em>italic</em> content.</p>
            <span>  Span with spaces  </span>
            <div>Nested content in section {i}</div>
        </div>
        """
    large_html += "</body></html>"
    
    print(f"Document size: ~{len(large_html)} characters")
    
    cs_soup = CutySoup(large_html)
    bs_soup = BeautifulSoup(large_html, 'html.parser')
    
    iterations = 100
    
    # Test 1: Basic text extraction
    print("\n1. Basic Text Extraction:")
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        cs_text = cs_soup.get_text()
    cs_time = time.perf_counter() - start_time
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        bs_text = bs_soup.get_text()
    bs_time = time.perf_counter() - start_time
    
    print(f"   CutySoup (C++): {cs_time:.4f}s")
    print(f"   BeautifulSoup4: {bs_time:.4f}s")
    if cs_time > 0:
        print(f"   Speedup: {bs_time/cs_time:.1f}x")
    
    # Test 2: Text with separator
    print("\n2. Text with Separator:")
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        cs_text = cs_soup.get_text(separator='|')
    cs_time = time.perf_counter() - start_time
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        bs_text = bs_soup.get_text(separator='|')
    bs_time = time.perf_counter() - start_time
    
    print(f"   CutySoup (C++): {cs_time:.4f}s")
    print(f"   BeautifulSoup4: {bs_time:.4f}s")
    if cs_time > 0:
        print(f"   Speedup: {bs_time/cs_time:.1f}x")
    
    # Test 3: Strings iteration (bulk vs generator)
    print("\n3. Strings Iteration (C++ bulk vs Python generator):")
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        cs_strings = list(cs_soup.strings)
    cs_time = time.perf_counter() - start_time
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        bs_strings = list(bs_soup.strings)
    bs_time = time.perf_counter() - start_time
    
    print(f"   CutySoup (C++ bulk): {cs_time:.4f}s")
    print(f"   BeautifulSoup4 (generator): {bs_time:.4f}s")
    if cs_time > 0:
        print(f"   Speedup: {bs_time/cs_time:.1f}x")

if __name__ == '__main__':
    test_cpp_text_methods()
    test_python_api_integration() 
    benchmark_text_processing()