#!/usr/bin/env python3
"""Test script for selector filtering in pywasot."""

from pywasot import parse_wasot_string
from pywasot import BlockStartedEvent, TextEvent, BlockFinishedEvent

def test_selectors():
    # Test Wasot content
    content = """.document
..section About
...paragraph :intro
This is the introduction paragraph.
...paragraph
This is another paragraph in the section.
../
..section Features .. lang=en
...paragraph
First feature description.
...quote
"The best thing about standards is that there are so many to choose from."
...paragraph
Second feature description.
../
./"""

    print("Test 1: No selectors (all events)")
    print("=" * 50)
    event_count = 0
    for event in parse_wasot_string(content):
        event_count += 1
    print(f"Total events without filtering: {event_count}")
    
    print("\nTest 2: Filter for paragraphs only")
    print("=" * 50)
    paragraph_events = []
    for event in parse_wasot_string(content, selectors=["paragraph"]):
        if isinstance(event, BlockStartedEvent):
            paragraph_events.append(f"Start: {event.meta_line.type_name}")
        elif isinstance(event, TextEvent):
            paragraph_events.append(f"Text: {event.lines[0][:30]}...")
        elif isinstance(event, BlockFinishedEvent):
            paragraph_events.append("End block")
    
    print(f"Paragraph events: {len(paragraph_events)}")
    for evt in paragraph_events:
        print(f"  - {evt}")
    
    print("\nTest 3: Filter for sections with lang attribute")
    print("=" * 50)
    lang_events = []
    for event in parse_wasot_string(content, selectors=["[lang]"]):
        if isinstance(event, BlockStartedEvent):
            lang_events.append(f"Start: {event.meta_line.type_name}")
        elif isinstance(event, TextEvent):
            lang_events.append(f"Text: {' '.join(event.lines)[:50]}...")
        elif isinstance(event, BlockFinishedEvent):
            lang_events.append("End block")
    
    print(f"Events with lang attribute: {len(lang_events)}")
    for evt in lang_events:
        print(f"  - {evt}")
    
    print("\nTest 4: Multiple selectors (paragraph OR quote)")
    print("=" * 50)
    multi_events = []
    for event in parse_wasot_string(content, selectors=["paragraph", "quote"]):
        if isinstance(event, BlockStartedEvent):
            multi_events.append(event.meta_line.type_name)
    
    print(f"Blocks matching paragraph or quote: {multi_events}")
    
    print("\nTest 5: Descendant selector (section paragraph)")
    print("=" * 50)
    descendant_count = 0
    for event in parse_wasot_string(content, selectors=["section paragraph"]):
        if isinstance(event, BlockStartedEvent):
            descendant_count += 1
            print(f"  Found paragraph in section at level {event.meta_line.level}")
    
    print(f"Total paragraphs inside sections: {descendant_count}")
    
    print("\nTest 6: ID selector")
    print("=" * 50)
    id_events = []
    for event in parse_wasot_string(content, selectors=["#intro"]):
        if isinstance(event, BlockStartedEvent):
            id_events.append("Found intro block")
        elif isinstance(event, TextEvent):
            id_events.append(f"Content: {event.lines[0]}")
    
    for evt in id_events:
        print(f"  {evt}")
    
    print("\n✅ All tests completed!")

if __name__ == "__main__":
    test_selectors()