#!/usr/bin/env python3
"""
Test script to verify the Tokopedia scraper installation
"""

import sys
import subprocess

def check_python_version():
    """Check if Python version is 3.7 or higher"""
    if sys.version_info < (3, 7):
        print("Error: Python 3.7 or higher is required")
        return False
    return True

def check_dependencies():
    """Check if required packages are installed"""
    required_packages = [
        'selenium',
        'webdriver_manager',
        'pandas',
        'requests'
    ]
    
    missing_packages = []
    
    for package in required_packages:
        try:
            __import__(package)
        except ImportError:
            missing_packages.append(package)
    
    if missing_packages:
        print(f"Missing packages: {', '.join(missing_packages)}")
        print("Please run: pip install -r requirements.txt")
        return False
    
    return True

def test_chrome_driver():
    """Test if ChromeDriver can be initialized"""
    try:
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        from webdriver_manager.chrome import ChromeDriverManager
        
        # Set up Chrome options
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        
        # Try to initialize the driver with compatible syntax
        # Handle different versions of selenium/webdriver-manager
        try:
            # Newer versions syntax
            driver = webdriver.Chrome(
                options=chrome_options
            )
        except TypeError:
            # Fallback for older versions
            driver = webdriver.Chrome(
                ChromeDriverManager().install(),
                options=chrome_options
            )
        driver.quit()
        return True
        
    except Exception as e:
        print(f"ChromeDriver test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("Testing Tokopedia Scraper Installation...")
    print("=" * 45)
    
    # Test Python version
    print("1. Checking Python version...")
    if not check_python_version():
        return False
    print("   ✓ Python version is compatible")
    
    # Test dependencies
    print("2. Checking dependencies...")
    if not check_dependencies():
        return False
    print("   ✓ All dependencies are installed")
    
    # Test ChromeDriver
    print("3. Testing ChromeDriver...")
    if not test_chrome_driver():
        return False
    print("   ✓ ChromeDriver is working correctly")
    
    print("\nAll tests passed! The scraper should work correctly.")
    print("\nTo use the scraper, run:")
    print("  python tokopedia_scraper.py")
    
    return True

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)