#!/usr/bin/env python3
"""
RockAuto Data Integration Demo
Shows how to integrate manually extracted RockAuto data into the vehicle database
"""

from vehicle_scraper.manual_input_simple import ManualDataInput
import sqlite3


def demonstrate_integration():
    """Demonstrate the integration of RockAuto data into the database"""
    print("RockAuto Data Integration Demo")
    print("==============================")
    
    # Create input tool with correct database path
    input_tool = ManualDataInput(db_path="vehicle_database/vehicle_database.db")
    
    # Example of data you might extract from RockAuto manually
    print("\nExample: Adding data that could be extracted from RockAuto")
    
    # Add some example vehicles that might be found on RockAuto
    rockauto_vehicles = [
        {"make": "Toyota", "model": "RAV4", "year": 2019, "engine": "2.5L 4-Cylinder"},
        {"make": "Honda", "model": "CR-V", "year": 2020, "engine": "1.5L Turbo"},
        {"make": "Ford", "model": "Escape", "year": 2021, "engine": "2.0L Turbo"},
        {"make": "Nissan", "model": "Rogue", "year": 2018, "engine": "2.5L 4-Cylinder"},
        {"make": "Chevrolet", "model": "Equinox", "year": 2020, "engine": "1.5L Turbo"},
    ]
    
    print(f"\nAdding {len(rockauto_vehicles)} vehicles to database:")
    for vehicle in rockauto_vehicles:
        print(f"  - {vehicle['year']} {vehicle['make']} {vehicle['model']} ({vehicle['engine']})")
    
    input_tool.add_multiple_vehicles(rockauto_vehicles)
    
    # Show the data in the database
    print("\nCurrent data in database:")
    conn = sqlite3.connect("vehicle_database/vehicle_database.db")
    cursor = conn.cursor()
    
    cursor.execute('''
        SELECT b.name as brand, m.name as model, y.year, e.name as engine 
        FROM model_year_engine mye
        JOIN models m ON mye.model_id = m.id
        JOIN brands b ON m.brand_id = b.id
        JOIN years y ON mye.year_id = y.id
        JOIN engines e ON mye.engine_id = e.id
        ORDER BY b.name, m.name, y.year
        LIMIT 15
    ''')
    
    results = cursor.fetchall()
    for i, (brand, model, year, engine) in enumerate(results, 1):
        print(f"  {i:2d}. {year} {brand} {model} - {engine}")
    
    if len(results) == 15:
        print("  ... (showing first 15 results)")
    
    conn.close()
    
    print(f"\nDatabase contains {len(results)} vehicle entries")
    
    # Show how to query specific data
    print("\nExample queries you can perform:")
    print("1. Find all Toyota models:")
    print("   In query interface: Search brand='Toyota'")
    
    print("2. Find all 2020 model year vehicles:")
    print("   In query interface: Search year=2020")
    
    print("3. Find all vehicles with 'Turbo' engines:")
    print("   In query interface: Search engine='Turbo'")
    
    print("\nTo continue adding RockAuto data:")
    print("1. Visit https://www.rockauto.com/catalog/")
    print("2. Browse manufacturers and note models/years/engines")
    print("3. Use ManualDataInput to add to your database")


def main():
    demonstrate_integration()
    
    print("\n" + "="*50)
    print("INTEGRATION DEMO COMPLETE")
    print("="*50)
    print("\nNext steps:")
    print("1. Manually browse RockAuto.com for vehicle data")
    print("2. Use the ManualDataInput class to add data")
    print("3. Query your database using the query interface")
    print("4. Build your comprehensive vehicle database")


if __name__ == "__main__":
    main()