"""
Script to add Audi vehicle data to the database.
This includes inserting the brand, models, and year/motor combinations.
"""
import sqlite3
from typing import List, Tuple


def connect_db(db_path: str = "vehicle_database/vehicle_database.db") -> sqlite3.Connection:
    """Connect to the vehicle database."""
    conn = sqlite3.connect(db_path)
    conn.execute("PRAGMA foreign_keys = ON")  # Enable foreign key constraints
    return conn


def get_or_insert_brand(conn: sqlite3.Connection, brand_name: str) -> int:
    """Get brand ID or insert if it doesn't exist."""
    cursor = conn.cursor()
    
    # Check if brand already exists
    cursor.execute("SELECT id FROM brands WHERE name = ?", (brand_name,))
    result = cursor.fetchone()
    
    if result:
        return result[0]  # Return existing ID
    
    # Insert new brand
    cursor.execute("INSERT INTO brands (name) VALUES (?)", (brand_name,))
    conn.commit()
    return cursor.lastrowid


def get_or_insert_model(conn: sqlite3.Connection, brand_id: int, model_name: str) -> int:
    """Get model ID or insert if it doesn't exist."""
    cursor = conn.cursor()
    
    # Check if model already exists for this brand
    cursor.execute(
        "SELECT id FROM models WHERE brand_id = ? AND name = ?", 
        (brand_id, model_name)
    )
    result = cursor.fetchone()
    
    if result:
        return result[0]  # Return existing ID
    
    # Insert new model
    cursor.execute(
        "INSERT INTO models (brand_id, name) VALUES (?, ?)", 
        (brand_id, model_name)
    )
    conn.commit()
    return cursor.lastrowid


def get_or_insert_engine(conn: sqlite3.Connection, engine_name: str) -> int:
    """Get engine ID or insert if it doesn't exist."""
    cursor = conn.cursor()
    
    # Check if engine already exists
    cursor.execute("SELECT id FROM engines WHERE name = ?", (engine_name,))
    result = cursor.fetchone()
    
    if result:
        return result[0]  # Return existing ID
    
    # Insert new engine
    cursor.execute("INSERT INTO engines (name) VALUES (?)", (engine_name,))
    conn.commit()
    return cursor.lastrowid


def get_or_insert_year(conn: sqlite3.Connection, year: int) -> int:
    """Get year ID or insert if it doesn't exist."""
    cursor = conn.cursor()
    
    # Check if year already exists
    cursor.execute("SELECT id FROM years WHERE year = ?", (year,))
    result = cursor.fetchone()
    
    if result:
        return result[0]  # Return existing ID
    
    # Insert new year
    cursor.execute("INSERT INTO years (year) VALUES (?)", (year,))
    conn.commit()
    return cursor.lastrowid


def insert_version_relationship(conn: sqlite3.Connection, model_id: int, year_id: int, engine_id: int) -> None:
    """Insert a version relationship if it doesn't exist."""
    cursor = conn.cursor()
    
    # Check if version relationship already exists
    cursor.execute(
        "SELECT id FROM model_year_engine WHERE model_id = ? AND year_id = ? AND engine_id = ?", 
        (model_id, year_id, engine_id)
    )
    result = cursor.fetchone()
    
    if not result:
        # Insert new version relationship
        cursor.execute(
            "INSERT INTO model_year_engine (model_id, year_id, engine_id) VALUES (?, ?, ?)", 
            (model_id, year_id, engine_id)
        )
        conn.commit()


def main():
    """Main function to add Audi data."""
    print("Adding Audi data...")
    
    # Connect to database
    conn = connect_db()
    
    # Insert brand
    brand_id = get_or_insert_brand(conn, "AUDI")
    print(f"Brand 'AUDI' inserted with ID: {brand_id}")
    
    # Define models
    models = [
        'A1',
        'A3',
        'A4',
        'A5',
        'A6',
        'A7',
        'A8',
        'Q2',
        'Q3',
        'Q4',
        'Q5',
        'Q6',
        'Q7',
        'Q8',
        'R8',
        'RS3',
        'RS5',
        'RS6',
        'RS7',
        'S3',
        'S4',
        'S5',
        'S6',
        'S7',
        'S8',
        'SQ5',
        'SQ7',
        'SQ8',
        'TT',
        'E-TRON',
        'RS E-TRON GT',
        'RS Q3',
        'RS Q8'
    ]
    
    # Insert models
    for model_name in models:
        model_id = get_or_insert_model(conn, brand_id, model_name)
    print(f"Models inserted for AUDI: {', '.join(models)}")
    
    # Define versions (model, year, engine)
    versions = [
        # 2026
        ('A3', 2026, '2.0L L4 Turbocharged'),
        ('A8', 2026, '3.0L V6 Turbocharged'),
        ('Q4', 2026, '2.0L L4 Turbocharged'),
        ('Q7', 2026, '2.0L L4 Turbocharged'),
        ('Q7', 2026, '3.0L V6 Turbocharged'),
        ('Q8', 2026, '3.0L V6 Turbocharged'),
        ('RS6', 2026, '4.0L V8 Turbocharged'),
        ('RS7', 2026, '4.0L V8 Turbocharged'),
        ('RS E-TRON GT', 2026, 'ELECTRIC'),
        ('RS Q8', 2026, '4.0L V8 Turbocharged'),
        ('S3', 2026, '2.0L L4 Turbocharged'),
        ('S4', 2026, '3.0L V6 Turbocharged'),
        ('S5', 2026, '3.0L V6 Turbocharged'),
        ('S6', 2026, '2.9L V6 Turbocharged'),
        ('S7', 2026, '2.9L V6 Turbocharged'),
        ('S8', 2026, '4.0L V8 Turbocharged'),
        ('SQ5', 2026, '3.0L V6 Turbocharged'),
        ('SQ7', 2026, '4.0L V8 Turbocharged'),
        ('SQ8', 2026, '4.0L V8 Turbocharged'),
        ('TT', 2026, '2.0L L4 Turbocharged'),
        ('TT', 2026, '2.5L L5 Turbocharged'),

        # 2025
        ('A1', 2025, '1.5L L4 Turbocharged'),
        ('A3', 2025, '1.4L L4 Turbocharged'),
        ('A3', 2025, '2.0L L4 Turbocharged'),
        ('A4', 2025, '2.0L L4 Turbocharged'),
        ('A5', 2025, '2.0L L4 Turbocharged'),
        ('A6', 2025, '2.0L L4 Turbocharged'),
        ('A6', 2025, '3.0L V6 Turbocharged'),
        ('A7', 2025, '2.0L L4 Turbocharged'),
        ('A8', 2025, '3.0L V6 Turbocharged'),
        ('A8', 2025, '4.0L V8 Turbocharged'),
        ('Q2', 2025, '1.4L L4 Turbocharged'),
        ('Q3', 2025, '1.4L L4 Turbocharged'),
        ('Q3', 2025, '2.0L L4 Turbocharged'),
        ('Q4', 2025, '2.0L L4 Turbocharged'),
        ('Q5', 2025, '2.0L L4 Turbocharged'),
        ('Q5', 2025, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q6', 2025, 'ELECTRIC'),
        ('Q7', 2025, '2.0L L4 Turbocharged'),
        ('Q7', 2025, '3.0L V6 Turbocharged'),
        ('Q8', 2025, '3.0L V6 Turbocharged'),
        ('R8', 2025, '5.2L V10'),
        ('RS3', 2025, '2.5L L5 Turbocharged'),
        ('RS5', 2025, '2.9L V6 Turbocharged'),
        ('RS6', 2025, '4.0L V8 Turbocharged'),
        ('RS7', 2025, '4.0L V8 Turbocharged'),
        ('RS E-TRON GT', 2025, 'ELECTRIC'),
        ('RS Q3', 2025, '2.5L L5 Turbocharged'),
        ('RS Q8', 2025, '4.0L V8 Turbocharged'),
        ('S3', 2025, '2.0L L4 Turbocharged'),
        ('S4', 2025, '3.0L V6 Turbocharged'),
        ('S5', 2025, '3.0L V6 Turbocharged'),
        ('S6', 2025, '2.9L V6 Turbocharged'),
        ('S7', 2025, '2.9L V6 Turbocharged'),
        ('S8', 2025, '4.0L V8 Turbocharged'),
        ('SQ5', 2025, '3.0L V6 Turbocharged'),
        ('SQ7', 2025, '4.0L V8 Turbocharged'),
        ('SQ8', 2025, '4.0L V8 Turbocharged'),
        ('TT', 2025, '2.0L L4 Turbocharged'),
        ('TT', 2025, '2.5L L5 Turbocharged'),
        ('E-TRON', 2025, 'ELECTRIC'),

        # 2024
        ('A1', 2024, '1.5L L4 Turbocharged'),
        ('A3', 2024, '1.4L L4 Turbocharged'),
        ('A3', 2024, '2.0L L4 Turbocharged'),
        ('A4', 2024, '2.0L L4 Turbocharged'),
        ('A5', 2024, '2.0L L4 Turbocharged'),
        ('A6', 2024, '2.0L L4 Turbocharged'),
        ('A6', 2024, '3.0L V6 Turbocharged'),
        ('A7', 2024, '2.0L L4 Turbocharged'),
        ('A8', 2024, '3.0L V6 Turbocharged'),
        ('A8', 2024, '4.0L V8 Turbocharged'),
        ('Q2', 2024, '1.4L L4 Turbocharged'),
        ('Q3', 2024, '1.4L L4 Turbocharged'),
        ('Q3', 2024, '2.0L L4 Turbocharged'),
        ('Q4', 2024, '2.0L L4 Turbocharged'),
        ('Q5', 2024, '2.0L L4 Turbocharged'),
        ('Q5', 2024, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q6', 2024, 'ELECTRIC'),
        ('Q7', 2024, '2.0L L4 Turbocharged'),
        ('Q7', 2024, '3.0L V6 Turbocharged'),
        ('Q8', 2024, '3.0L V6 Turbocharged'),
        ('R8', 2024, '5.2L V10'),
        ('RS3', 2024, '2.5L L5 Turbocharged'),
        ('RS5', 2024, '2.9L V6 Turbocharged'),
        ('RS6', 2024, '4.0L V8 Turbocharged'),
        ('RS7', 2024, '4.0L V8 Turbocharged'),
        ('RS E-TRON GT', 2024, 'ELECTRIC'),
        ('RS Q3', 2024, '2.5L L5 Turbocharged'),
        ('RS Q8', 2024, '4.0L V8 Turbocharged'),
        ('S3', 2024, '2.0L L4 Turbocharged'),
        ('S4', 2024, '3.0L V6 Turbocharged'),
        ('S5', 2024, '3.0L V6 Turbocharged'),
        ('S6', 2024, '2.9L V6 Turbocharged'),
        ('S7', 2024, '2.9L V6 Turbocharged'),
        ('S8', 2024, '4.0L V8 Turbocharged'),
        ('SQ5', 2024, '3.0L V6 Turbocharged'),
        ('SQ7', 2024, '4.0L V8 Turbocharged'),
        ('SQ8', 2024, '4.0L V8 Turbocharged'),
        ('TT', 2024, '2.0L L4 Turbocharged'),
        ('TT', 2024, '2.5L L5 Turbocharged'),
        ('E-TRON', 2024, 'ELECTRIC'),

        # 2023
        ('A1', 2023, '1.5L L4 Turbocharged'),
        ('A3', 2023, '1.4L L4 Turbocharged'),
        ('A3', 2023, '2.0L L4 Turbocharged'),
        ('A4', 2023, '2.0L L4 Turbocharged'),
        ('A5', 2023, '2.0L L4 Turbocharged'),
        ('A6', 2023, '2.0L L4 Turbocharged'),
        ('A6', 2023, '3.0L V6 Turbocharged'),
        ('A7', 2023, '2.0L L4 Turbocharged'),
        ('A8', 2023, '3.0L V6 Turbocharged'),
        ('A8', 2023, '4.0L V8 Turbocharged'),
        ('Q2', 2023, '1.4L L4 Turbocharged'),
        ('Q3', 2023, '1.4L L4 Turbocharged'),
        ('Q3', 2023, '2.0L L4 Turbocharged'),
        ('Q4', 2023, '2.0L L4 Turbocharged'),
        ('Q5', 2023, '2.0L L4 Turbocharged'),
        ('Q5', 2023, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q6', 2023, 'ELECTRIC'),
        ('Q7', 2023, '2.0L L4 Turbocharged'),
        ('Q7', 2023, '3.0L V6 Turbocharged'),
        ('Q8', 2023, '3.0L V6 Turbocharged'),
        ('R8', 2023, '5.2L V10'),
        ('RS3', 2023, '2.5L L5 Turbocharged'),
        ('RS5', 2023, '2.9L V6 Turbocharged'),
        ('RS6', 2023, '4.0L V8 Turbocharged'),
        ('RS7', 2023, '4.0L V8 Turbocharged'),
        ('RS E-TRON GT', 2023, 'ELECTRIC'),
        ('RS Q3', 2023, '2.5L L5 Turbocharged'),
        ('RS Q8', 2023, '4.0L V8 Turbocharged'),
        ('S3', 2023, '2.0L L4 Turbocharged'),
        ('S4', 2023, '3.0L V6 Turbocharged'),
        ('S5', 2023, '3.0L V6 Turbocharged'),
        ('S6', 2023, '2.9L V6 Turbocharged'),
        ('S7', 2023, '2.9L V6 Turbocharged'),
        ('S8', 2023, '4.0L V8 Turbocharged'),
        ('SQ5', 2023, '3.0L V6 Turbocharged'),
        ('SQ7', 2023, '4.0L V8 Turbocharged'),
        ('SQ8', 2023, '4.0L V8 Turbocharged'),
        ('TT', 2023, '2.0L L4 Turbocharged'),
        ('TT', 2023, '2.5L L5 Turbocharged'),
        ('E-TRON', 2023, 'ELECTRIC'),

        # 2022
        ('A1', 2022, '1.5L L4 Turbocharged'),
        ('A3', 2022, '1.4L L4 Turbocharged'),
        ('A3', 2022, '2.0L L4 Turbocharged'),
        ('A4', 2022, '2.0L L4 Turbocharged'),
        ('A5', 2022, '2.0L L4 Turbocharged'),
        ('A6', 2022, '2.0L L4 Turbocharged'),
        ('A6', 2022, '3.0L V6 Turbocharged'),
        ('A7', 2022, '2.0L L4 Turbocharged'),
        ('A8', 2022, '3.0L V6 Turbocharged'),
        ('A8', 2022, '4.0L V8 Turbocharged'),
        ('Q2', 2022, '1.4L L4 Turbocharged'),
        ('Q3', 2022, '1.4L L4 Turbocharged'),
        ('Q3', 2022, '2.0L L4 Turbocharged'),
        ('Q4', 2022, '2.0L L4 Turbocharged'),
        ('Q5', 2022, '2.0L L4 Turbocharged'),
        ('Q5', 2022, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q6', 2022, 'ELECTRIC'),
        ('Q7', 2022, '2.0L L4 Turbocharged'),
        ('Q7', 2022, '3.0L V6 Turbocharged'),
        ('Q8', 2022, '3.0L V6 Turbocharged'),
        ('R8', 2022, '5.2L V10'),
        ('RS3', 2022, '2.5L L5 Turbocharged'),
        ('RS5', 2022, '2.9L V6 Turbocharged'),
        ('RS6', 2022, '4.0L V8 Turbocharged'),
        ('RS7', 2022, '4.0L V8 Turbocharged'),
        ('RS E-TRON GT', 2022, 'ELECTRIC'),
        ('RS Q3', 2022, '2.5L L5 Turbocharged'),
        ('RS Q8', 2022, '4.0L V8 Turbocharged'),
        ('S3', 2022, '2.0L L4 Turbocharged'),
        ('S4', 2022, '3.0L V6 Turbocharged'),
        ('S5', 2022, '3.0L V6 Turbocharged'),
        ('S6', 2022, '2.9L V6 Turbocharged'),
        ('S7', 2022, '2.9L V6 Turbocharged'),
        ('S8', 2022, '4.0L V8 Turbocharged'),
        ('SQ5', 2022, '3.0L V6 Turbocharged'),
        ('SQ7', 2022, '4.0L V8 Turbocharged'),
        ('SQ8', 2022, '4.0L V8 Turbocharged'),
        ('TT', 2022, '2.0L L4 Turbocharged'),
        ('TT', 2022, '2.5L L5 Turbocharged'),
        ('E-TRON', 2022, 'ELECTRIC')
    ]
    
    # Insert version relationships
    for model_name, year, engine in versions:
        model_id = get_or_insert_model(conn, brand_id, model_name)
        year_id = get_or_insert_year(conn, year)
        engine_id = get_or_insert_engine(conn, engine)
        insert_version_relationship(conn, model_id, year_id, engine_id)
    
    print(f"Inserted {len(versions)} version relationships for AUDI vehicles")
    
    # Close connection
    conn.close()
    print("Audi data added successfully!")


if __name__ == "__main__":
    main()