"""
Script to add extended Audi vehicle data (2012-2016) to the database.
This includes additional 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 extended Audi data (2012-2016)."""
    print("Adding extended Audi data (2012-2016)...")
    
    # Connect to database
    conn = connect_db()
    
    # Get brand ID (should already exist)
    brand_id = get_or_insert_brand(conn, "AUDI")
    print(f"Brand 'AUDI' has ID: {brand_id}")
    
    # Define additional models that might need to be added
    additional_models = [
        'ALLROAD'
    ]
    
    # Insert additional models if they don't exist
    for model_name in additional_models:
        model_id = get_or_insert_model(conn, brand_id, model_name)
    
    # Define versions (model, year, engine) for years 2012-2016
    versions = [
        # 2016
        ('A1', 2016, '1.4L L4 Turbocharged'),
        ('A1', 2016, '1.8L L4 Turbocharged'),
        ('A3', 2016, '1.4L L4 ELECTRIC/GAS Turbocharged'),
        ('A3', 2016, '1.4L L4 Turbocharged'),
        ('A3', 2016, '1.8L L4 Turbocharged'),
        ('A3', 2016, '2.0L L4 DIESEL Turbocharged'),
        ('A3', 2016, '2.0L L4 Turbocharged'),
        ('A4', 2016, '1.8L L4 Turbocharged'),
        ('A4', 2016, '2.0L L4 Turbocharged'),
        ('A4 ALLROAD', 2016, '2.0L L4 Turbocharged'),
        ('A5', 2016, '2.0L L4 Turbocharged'),
        ('A5', 2016, '3.0L V6 Supercharged'),
        ('A6', 2016, '1.8L L4 Turbocharged'),
        ('A6', 2016, '2.0L L4 Turbocharged'),
        ('A6', 2016, '3.0L V6 DIESEL Turbocharged'),
        ('A6', 2016, '3.0L V6 Supercharged'),
        ('A7', 2016, '2.0L L4 Turbocharged'),
        ('A7', 2016, '3.0L V6 DIESEL Turbocharged'),
        ('A7', 2016, '3.0L V6 Supercharged'),
        ('A8', 2016, '3.0L V6 DIESEL Turbocharged'),
        ('A8', 2016, '3.0L V6 Supercharged'),
        ('A8', 2016, '4.0L V8 Turbocharged'),
        ('A8', 2016, '6.3L W12 Turbocharged'),
        ('ALLROAD', 2016, '2.0L L4 Turbocharged'),
        ('Q3', 2016, '1.4L L4 Turbocharged'),
        ('Q3', 2016, '2.0L L4 Turbocharged'),
        ('Q5', 2016, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q5', 2016, '2.0L L4 Turbocharged'),
        ('Q5', 2016, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2016, '2.0L L4 Turbocharged'),
        ('Q7', 2016, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2016, '3.0L V6 Supercharged'),
        ('Q8', 2016, '3.0L V6 Turbocharged'),
        ('R8', 2016, '4.2L V8'),
        ('R8', 2016, '5.2L V10'),
        ('RS5', 2016, '4.2L V8'),
        ('RS7', 2016, '4.0L V8 Turbocharged'),
        ('S3', 2016, '2.0L L4 Turbocharged'),
        ('S4', 2016, '3.0L V6 Supercharged'),
        ('S5', 2016, '3.0L V6 Supercharged'),
        ('S6', 2016, '4.0L V8 Turbocharged'),
        ('S7', 2016, '4.0L V8 Turbocharged'),
        ('S8', 2016, '4.0L V8 Turbocharged'),
        ('SQ5', 2016, '3.0L V6 Supercharged'),
        ('TT', 2016, '1.8L L4 Turbocharged'),
        ('TT', 2016, '2.0L L4 Turbocharged'),

        # 2015
        ('A1', 2015, '1.4L L4 Turbocharged'),
        ('A3', 2015, '1.4L L4 Turbocharged'),
        ('A3', 2015, '1.8L L4 Turbocharged'),
        ('A3', 2015, '2.0L L4 DIESEL Turbocharged'),
        ('A3', 2015, '2.0L L4 Turbocharged'),
        ('A4', 2015, '1.8L L4 Turbocharged'),
        ('A4', 2015, '2.0L L4 Turbocharged'),
        ('A4 ALLROAD', 2015, '2.0L L4 Turbocharged'),
        ('A5', 2015, '1.8L L4 Turbocharged'),
        ('A5', 2015, '2.0L L4 Turbocharged'),
        ('A5', 2015, '3.0L V6 Supercharged'),
        ('A6', 2015, '2.0L L4 Turbocharged'),
        ('A6', 2015, '3.0L V6 DIESEL Turbocharged'),
        ('A6', 2015, '3.0L V6 Supercharged'),
        ('A7', 2015, '2.0L L4 Turbocharged'),
        ('A7', 2015, '3.0L V6 DIESEL Turbocharged'),
        ('A7', 2015, '3.0L V6 Supercharged'),
        ('A8', 2015, '3.0L V6 DIESEL Turbocharged'),
        ('A8', 2015, '3.0L V6 Supercharged'),
        ('A8', 2015, '4.0L V8 Turbocharged'),
        ('A8', 2015, '6.3L W12'),
        ('ALLROAD', 2015, '2.0L L4 Turbocharged'),
        ('Q3', 2015, '2.0L L4 Turbocharged'),
        ('Q5', 2015, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q5', 2015, '2.0L L4 Turbocharged'),
        ('Q5', 2015, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2015, '2.0L L4 Turbocharged'),
        ('Q7', 2015, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2015, '3.0L V6 Supercharged'),
        ('Q8', 2015, '3.0L V6 Turbocharged'),
        ('R8', 2015, '4.2L V8'),
        ('R8', 2015, '5.2L V10'),
        ('RS5', 2015, '4.2L V8'),
        ('RS7', 2015, '4.0L V8 Turbocharged'),
        ('S3', 2015, '2.0L L4 Turbocharged'),
        ('S4', 2015, '3.0L V6 Supercharged'),
        ('S5', 2015, '3.0L V6 Supercharged'),
        ('S6', 2015, '4.0L V8 Turbocharged'),
        ('S7', 2015, '4.0L V8 Turbocharged'),
        ('S8', 2015, '4.0L V8 Turbocharged'),
        ('SQ5', 2015, '3.0L V6 Supercharged'),
        ('TT', 2015, '1.8L L4 Turbocharged'),
        ('TT', 2015, '2.0L L4 Turbocharged'),

        # 2014
        ('A1', 2014, '1.4L L4 Turbocharged'),
        ('A3', 2014, '1.4L L4 Turbocharged'),
        ('A3', 2014, '1.8L L4 Turbocharged'),
        ('A4', 2014, '1.8L L4 Turbocharged'),
        ('A4', 2014, '2.0L L4 Turbocharged'),
        ('A4 ALLROAD', 2014, '2.0L L4 Turbocharged'),
        ('A5', 2014, '1.8L L4 Turbocharged'),
        ('A5', 2014, '2.0L L4 Turbocharged'),
        ('A5', 2014, '3.0L V6 Supercharged'),
        ('A6', 2014, '2.0L L4 Turbocharged'),
        ('A6', 2014, '3.0L V6 DIESEL Turbocharged'),
        ('A6', 2014, '3.0L V6 Supercharged'),
        ('A7', 2014, '2.0L L4 Turbocharged'),
        ('A7', 2014, '3.0L V6 DIESEL Turbocharged'),
        ('A7', 2014, '3.0L V6 Supercharged'),
        ('A8', 2014, '3.0L V6 DIESEL Turbocharged'),
        ('A8', 2014, '3.0L V6 Supercharged'),
        ('A8', 2014, '4.0L V8 Turbocharged'),
        ('A8', 2014, '6.3L W12'),
        ('ALLROAD', 2014, '2.0L L4 Turbocharged'),
        ('Q3', 2014, '2.0L L4 Turbocharged'),
        ('Q5', 2014, '2.0L L4 Turbocharged'),
        ('Q5', 2014, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2014, '2.0L L4 Turbocharged'),
        ('Q7', 2014, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2014, '3.0L V6 Supercharged'),
        ('Q8', 2014, '3.0L V6 Turbocharged'),
        ('R8', 2014, '4.2L V8'),
        ('R8', 2014, '5.2L V10'),
        ('RS5', 2014, '4.2L V8'),
        ('RS7', 2014, '4.0L V8 Turbocharged'),
        ('S3', 2014, '2.0L L4 Turbocharged'),
        ('S4', 2014, '3.0L V6 Supercharged'),
        ('S5', 2014, '3.0L V6 Supercharged'),
        ('S6', 2014, '4.0L V8 Turbocharged'),
        ('S7', 2014, '4.0L V8 Turbocharged'),
        ('S8', 2014, '4.0L V8 Turbocharged'),
        ('SQ5', 2014, '3.0L V6 Supercharged'),
        ('TT', 2014, '1.8L L4 Turbocharged'),
        ('TT', 2014, '2.0L L4 Turbocharged'),

        # 2013
        ('A1', 2013, '1.4L L4 Turbocharged'),
        ('A3', 2013, '1.4L L4 Turbocharged'),
        ('A3', 2013, '1.8L L4 Turbocharged'),
        ('A3', 2013, '2.0L L4 DIESEL Turbocharged'),
        ('A3', 2013, '2.0L L4 Turbocharged'),
        ('A4', 2013, '1.8L L4 Turbocharged'),
        ('A4', 2013, '2.0L L4 Turbocharged'),
        ('A5', 2013, '1.8L L4 Turbocharged'),
        ('A5', 2013, '2.0L L4 Turbocharged'),
        ('A5', 2013, '3.0L V6 Supercharged'),
        ('A6', 2013, '2.0L L4 Turbocharged'),
        ('A6', 2013, '2.8L V6'),
        ('A6', 2013, '3.0L V6 DIESEL Turbocharged'),
        ('A6', 2013, '3.0L V6 Supercharged'),
        ('A7', 2013, '3.0L V6 Supercharged'),
        ('A8', 2013, '3.0L V6 Supercharged'),
        ('A8', 2013, '4.0L V8 Turbocharged'),
        ('A8', 2013, '6.3L W12'),
        ('ALLROAD', 2013, '2.0L L4 Turbocharged'),
        ('Q3', 2013, '2.0L L4 Turbocharged'),
        ('Q5', 2013, '2.0L L4 ELECTRIC/GAS Turbocharged'),
        ('Q5', 2013, '2.0L L4 Turbocharged'),
        ('Q5', 2013, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2013, '2.0L L4 Turbocharged'),
        ('Q7', 2013, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2013, '3.0L V6 Supercharged'),
        ('Q8', 2013, '3.0L V6 Turbocharged'),
        ('R8', 2013, '4.2L V8'),
        ('R8', 2013, '5.2L V10'),
        ('RS5', 2013, '4.2L V8'),
        ('RS7', 2013, '4.0L V8 Turbocharged'),
        ('S3', 2013, '2.0L L4 Turbocharged'),
        ('S4', 2013, '3.0L V6 Supercharged'),
        ('S5', 2013, '3.0L V6 Supercharged'),
        ('S6', 2013, '4.0L V8 Turbocharged'),
        ('S7', 2013, '4.0L V8 Turbocharged'),
        ('S8', 2013, '4.0L V8 Turbocharged'),
        ('SQ5', 2013, '3.0L V6 Supercharged'),
        ('TT', 2013, '1.8L L4 Turbocharged'),
        ('TT', 2013, '2.0L L4 Turbocharged'),
        ('TT', 2013, '2.5L L5 Turbocharged'),

        # 2012
        ('A1', 2012, '1.4L L4 Turbocharged'),
        ('A3', 2012, '1.4L L4 Turbocharged'),
        ('A3', 2012, '1.8L L4 Turbocharged'),
        ('A3', 2012, '2.0L L4 DIESEL Turbocharged'),
        ('A3', 2012, '2.0L L4 Turbocharged'),
        ('A4', 2012, '1.8L L4 Turbocharged'),
        ('A4', 2012, '2.0L L4 Turbocharged'),
        ('A5', 2012, '1.8L L4 Turbocharged'),
        ('A5', 2012, '2.0L L4 Turbocharged'),
        ('A5', 2012, '3.0L V6 Supercharged'),
        ('A6', 2012, '2.0L L4 Turbocharged'),
        ('A6', 2012, '2.8L V6'),
        ('A6', 2012, '3.0L V6 DIESEL Turbocharged'),
        ('A6', 2012, '3.0L V6 Supercharged'),
        ('A7', 2012, '3.0L V6 Supercharged'),
        ('A8', 2012, '3.0L V6 Supercharged'),
        ('A8', 2012, '4.0L V8 Turbocharged'),
        ('A8', 2012, '6.3L W12'),
        ('Q3', 2012, '2.0L L4 Turbocharged'),
        ('Q5', 2012, '2.0L L4 Turbocharged'),
        ('Q5', 2012, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2012, '2.0L L4 Turbocharged'),
        ('Q7', 2012, '3.0L V6 DIESEL Turbocharged'),
        ('Q7', 2012, '3.0L V6 Supercharged'),
        ('Q8', 2012, '3.0L V6 Turbocharged'),
        ('R8', 2012, '4.2L V8'),
        ('R8', 2012, '5.2L V10'),
        ('RS5', 2012, '4.2L V8'),
        ('RS7', 2012, '4.0L V8 Turbocharged'),
        ('S3', 2012, '2.0L L4 Turbocharged'),
        ('S4', 2012, '3.0L V6 Supercharged'),
        ('S5', 2012, '3.0L V6 Supercharged'),
        ('S6', 2012, '4.0L V8 Turbocharged'),
        ('S7', 2012, '4.0L V8 Turbocharged'),
        ('S8', 2012, '4.0L V8 Turbocharged'),
        ('SQ5', 2012, '3.0L V6 Supercharged'),
        ('TT', 2012, '1.8L L4 Turbocharged'),
        ('TT', 2012, '2.0L L4 Turbocharged'),
        ('TT', 2012, '2.5L L5 Turbocharged')
    ]
    
    # 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)} additional version relationships for AUDI vehicles (2012-2016)")
    
    # Close connection
    conn.close()
    print("Extended Audi data (2012-2016) added successfully!")


if __name__ == "__main__":
    main()