#!/usr/bin/env python3
"""Verify the actual performance issues mentioned in the review."""

import time
import platform
from cutysoup import CutySoup
from bs4 import BeautifulSoup

print(f"Testing on: {platform.system()} {platform.release()}")
print("=" * 50)

# Test HTML with text content
test_html = """
<html>
<body>
    <div>
        <p>First paragraph with some text content here.</p>
        <p>Second paragraph with more text content.</p>
        <div>
            <span>Nested text content</span>
            <span>More nested content</span>
        </div>
        <p>Third paragraph with final text.</p>
    </div>
    <article>
        <h1>Article Title</h1>
        <p>Article paragraph 1</p>
        <p>Article paragraph 2</p>
        <p>Article paragraph 3</p>
    </article>
</body>
</html>
"""

def test_text_extraction():
    """Test text extraction performance - the alleged 100x slowdown."""
    print("🔍 Testing Text Extraction Performance")
    
    # Parse with both libraries
    cs_soup = CutySoup(test_html)
    bs_soup = BeautifulSoup(test_html, 'html.parser')
    
    # Test CutySoup text extraction
    iterations = 1000
    
    start_time = time.perf_counter()
    for _ in range(iterations):
        try:
            text = cs_soup.get_text()
        except Exception as e:
            print(f"❌ CutySoup get_text() failed: {e}")
            return
    cs_time = time.perf_counter() - start_time
    
    # Test BeautifulSoup text extraction
    start_time = time.perf_counter() 
    for _ in range(iterations):
        text = bs_soup.get_text()
    bs_time = time.perf_counter() - start_time
    
    print(f"CutySoup: {cs_time:.4f}s ({cs_time/iterations*1000:.2f}ms per call)")
    print(f"BeautifulSoup4: {bs_time:.4f}s ({bs_time/iterations*1000:.2f}ms per call)")
    
    if cs_time > 0 and bs_time > 0:
        ratio = cs_time / bs_time
        print(f"Ratio: {ratio:.1f}x ({'SLOWER' if ratio > 1 else 'FASTER'})")
    
    return cs_time, bs_time

def test_navigation():
    """Test navigation functionality - allegedly broken generators."""
    print("\n🔍 Testing Navigation Functionality")
    
    cs_soup = CutySoup(test_html)
    
    try:
        root_div = cs_soup.find('div')
        if root_div:
            # Test descendants
            print("Testing descendants...")
            descendants = list(root_div.descendants)
            print(f"✅ descendants: {len(descendants)} items")
            
            # Test next_siblings
            print("Testing next_siblings...")
            first_p = root_div.find('p')
            if first_p:
                siblings = list(first_p.next_siblings)
                print(f"✅ next_siblings: {len(siblings)} items")
            
            print("✅ Navigation working")
            return True
            
    except Exception as e:
        print(f"❌ Navigation failed: {e}")
        return False

def test_css_selectors():
    """Test CSS selector performance."""
    print("\n🔍 Testing CSS Selector Performance")
    
    cs_soup = CutySoup(test_html)
    bs_soup = BeautifulSoup(test_html, 'html.parser')
    
    iterations = 1000
    
    # Test CutySoup CSS
    start_time = time.perf_counter()
    for _ in range(iterations):
        try:
            results = cs_soup.select('p')
        except Exception as e:
            print(f"❌ CutySoup CSS failed: {e}")
            return
    cs_time = time.perf_counter() - start_time
    
    # Test BeautifulSoup CSS
    start_time = time.perf_counter()
    for _ in range(iterations):
        results = bs_soup.select('p')
    bs_time = time.perf_counter() - start_time
    
    print(f"CutySoup CSS: {cs_time:.4f}s")
    print(f"BeautifulSoup4 CSS: {bs_time:.4f}s")
    
    if cs_time > 0 and bs_time > 0:
        ratio = cs_time / bs_time  
        print(f"Ratio: {ratio:.1f}x ({'SLOWER' if ratio > 1 else 'FASTER'})")

def test_with_larger_document():
    """Test with a larger document to see scaling issues."""
    print("\n🔍 Testing with Larger Document")
    
    # Generate larger HTML
    large_html = "<html><body>"
    for i in range(100):
        large_html += f"""
        <div class="section-{i}">
            <h2>Section {i}</h2>
            <p>Paragraph 1 in section {i} with content.</p>
            <p>Paragraph 2 in section {i} with more content.</p>
            <p>Paragraph 3 in section {i} with final content.</p>
        </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')
    
    # Test text extraction on large doc
    print("Large document text extraction:")
    
    start_time = time.perf_counter()
    try:
        cs_text = cs_soup.get_text()
        cs_text_time = time.perf_counter() - start_time
        print(f"CutySoup: {cs_text_time:.4f}s")
    except Exception as e:
        print(f"❌ CutySoup failed: {e}")
        return
        
    start_time = time.perf_counter()
    bs_text = bs_soup.get_text()
    bs_text_time = time.perf_counter() - start_time
    print(f"BeautifulSoup4: {bs_text_time:.4f}s")
    
    if cs_text_time > 0 and bs_text_time > 0:
        ratio = cs_text_time / bs_text_time
        print(f"Ratio: {ratio:.1f}x ({'SLOWER' if ratio > 1 else 'FASTER'})")

if __name__ == '__main__':
    test_text_extraction()
    test_navigation()
    test_css_selectors()
    test_with_larger_document()