#!/usr/bin/env python3
"""
Demo script showing how to use the Whoosh-based code search system.
This demonstrates practical usage of the search functionality.
"""

import sys
from pathlib import Path

# Add current directory to path
sys.path.insert(0, '.')

from tools.intelligent_code_search import WhooshCodeSearch

def demo_search_functionality():
    """Demonstrate the search functionality."""
    print("🚀 Whoosh Code Search Demo")
    print("=" * 40)
    
    # Initialize search system
    print("📚 Initializing search system...")
    search = WhooshCodeSearch('.')
    print("✅ Search system ready")
    
    # Demo searches
    demos = [
        ("🔍 Search for 'main' functions", lambda: search.find_functions_intelligent('main', max_results=3)),
        ("🏛️ Search for 'Workflow' classes", lambda: search.find_classes_intelligent('Workflow', max_results=3)),
        ("📦 Search for 'pathlib' imports", lambda: search.find_imports_intelligent('pathlib', max_results=3)),
        ("💭 Semantic search for 'agent'", lambda: search.search_code_intelligent('agent', search_type='semantic', max_results=3)),
        ("🎯 Exact search for 'def __init__'", lambda: search.search_code_intelligent('def __init__', search_type='exact', max_results=3)),
    ]
    
    for description, search_func in demos:
        print(f"\n{description}")
        print("-" * len(description))
        
        try:
            results = search_func()
            if results:
                for i, result in enumerate(results, 1):
                    print(f"{i}. 📄 {result.file_path}:{result.line_number}")
                    print(f"   Language: {result.language}")
                    print(f"   Content: {result.line_content.strip()}")
                    print(f"   Type: {result.match_type} (Score: {result.score:.2f})")
                    if result.context_before:
                        print(f"   Context: ...{result.context_before[-1].strip()}")
                    print()
            else:
                print("   No results found")
        except Exception as e:
            print(f"   Error: {e}")
    
    # Show formatting capabilities
    print("\n📋 Formatted Results Demo")
    print("-" * 25)
    
    try:
        results = search.search_code_intelligent('logger', max_results=5)
        formatted = search.format_results(results, max_display=3)
        print(formatted)
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    demo_search_functionality()