#!/usr/bin/env python3
"""
Test Brave browser integration with Pokemon Discovery
"""

import sys
import os

try:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    print("✓ Selenium and webdriver-manager are available")
    
    # Check if Brave is available
    if not os.path.exists('/usr/bin/brave'):
        print("✗ Brave browser not found at /usr/bin/brave")
        sys.exit(1)
    
    print("✓ Brave browser found at /usr/bin/brave")
    
    # Get Brave version
    import subprocess
    try:
        result = subprocess.run(['/usr/bin/brave', '--version'], 
                              capture_output=True, text=True, timeout=5)
        brave_version = result.stdout.strip()
        print(f"✓ {brave_version}")
    except:
        print("⚠ Could not get Brave version")
    
    # Test ChromeDriver compatibility
    print("\nTesting ChromeDriver compatibility...")
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.binary_location = '/usr/bin/brave'
    
    try:
        service = Service(ChromeDriverManager().install())
        driver = webdriver.Chrome(service=service, options=options)
        
        # Simple test page
        driver.get("data:text/html,<html><body><h1>Test</h1></body></html>")
        title = driver.title
        driver.quit()
        
        print("✓ Brave + ChromeDriver test successful!")
        print("✓ Pokemon Discovery is ready to use Brave for dynamic content")
        
    except Exception as e:
        print(f"✗ ChromeDriver compatibility issue: {e}")
        print("\n💡 Solutions:")
        print("1. Update ChromeDriver: pip install --upgrade webdriver-manager")
        print("2. Install matching ChromeDriver version manually")
        print("3. Use Firefox with geckodriver as alternative")
        print("\nNote: The main PDF generation functionality works without browser automation")

except ImportError as e:
    print(f"✗ Missing dependency: {e}")
    print("Run: pip install selenium webdriver-manager")
    sys.exit(1)

print("\n🎯 Test completed!")