"""
Script to add Aston Martin 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 Aston Martin data."""
    print("Adding Aston Martin data...")
    
    # Connect to database
    conn = connect_db()
    
    # Insert brand
    brand_id = get_or_insert_brand(conn, "ASTON MARTIN")
    print(f"Brand 'ASTON MARTIN' inserted with ID: {brand_id}")
    
    # Define models
    models = [
        'LAGONDA',
        'ZAGATO',
        'DB7',
        'VANTAGE',
        'VIRAGE',
        'VANQUISH',
        'DB9',
        'DBS',
        'RAPIDE',
        'ONE-77',
        'V12 VANTAGE',
        'DB11',
        'DBX',
        'DB12',
        'VALHALLA',
        'VALOUR',
        'ARRA'
    ]
    
    # Insert models
    for model_name in models:
        model_id = get_or_insert_model(conn, brand_id, model_name)
    print(f"Models inserted for ASTON MARTIN: {', '.join(models)}")
    
    # Define versions (model, year, engine)
    versions = [
        # 2025
        ('DB12', 2025, '4.0L V8 Turbocharged'),
        ('DBX707', 2025, '4.0L V8 Turbocharged'),
        ('VALHALLA', 2025, '4.0L V8 ELECTRIC/GAS Turbocharged'),
        ('VANQUISH', 2025, '5.2L V12 Turbocharged'),
        ('VANTAGE', 2025, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2025, '5.2L V12 Turbocharged'),

        # 2024
        ('DB12', 2024, '4.0L V8 Turbocharged'),
        ('DBS', 2024, '5.2L V12 Turbocharged'),
        ('DBX', 2024, '4.0L V8 Turbocharged'),
        ('DBX707', 2024, '4.0L V8 Turbocharged'),
        ('VALOUR', 2024, '5.2L V12 Turbocharged'),
        ('VANTAGE', 2024, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2024, '5.2L V12 Turbocharged'),

        # 2023
        ('DB11', 2023, '4.0L V8 Turbocharged'),
        ('DBS', 2023, '5.2L V12 Turbocharged'),
        ('DBX', 2023, '4.0L V8 Turbocharged'),
        ('DBX707', 2023, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2023, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2023, '5.2L V12 Turbocharged'),

        # 2022
        ('DB11', 2022, '4.0L V8 Turbocharged'),
        ('DBS', 2022, '5.2L V12 Turbocharged'),
        ('DBX', 2022, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2022, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2022, '5.2L V12 Turbocharged'),

        # 2021
        ('DB11', 2021, '4.0L V8 Turbocharged'),
        ('DBS', 2021, '5.2L V12 Turbocharged'),
        ('DBX', 2021, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2021, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2021, '5.2L V12 Turbocharged'),

        # 2020
        ('DB11', 2020, '4.0L V8 Turbocharged'),
        ('DBS', 2020, '5.2L V12 Turbocharged'),
        ('VANTAGE', 2020, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2020, '5.2L V12 Turbocharged'),

        # 2019
        ('DB11', 2019, '4.0L V8 Turbocharged'),
        ('DBS', 2019, '5.2L V12 Turbocharged'),
        ('RAPIDE', 2019, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2019, '4.0L V8 Turbocharged'),

        # 2018
        ('DB11', 2018, '4.0L V8 Turbocharged'),
        ('RAPIDE', 2018, '4.0L V8 Turbocharged'),
        ('VANTAGE', 2018, '4.0L V8 Turbocharged'),

        # 2017
        ('DB11', 2017, '5.2L V12 Turbocharged'),
        ('RAPIDE', 2017, '6.0L V12'),
        ('V12 VANTAGE', 2017, '6.0L V12'),
        ('V8 VANTAGE', 2017, '4.7L V8'),
        ('VANQUISH', 2017, '6.0L V12'),

        # 2016
        ('DB9', 2016, '6.0L V12'),
        ('RAPIDE', 2016, '6.0L V12'),
        ('V12 VANTAGE', 2016, '6.0L V12'),
        ('V8 VANTAGE', 2016, '4.7L V8'),
        ('VANQUISH', 2016, '6.0L V12'),

        # 2015
        ('DB9', 2015, '6.0L V12'),
        ('RAPIDE', 2015, '6.0L V12'),
        ('V12 VANTAGE', 2015, '6.0L V12'),
        ('V8 VANTAGE', 2015, '4.7L V8'),
        ('VANQUISH', 2015, '6.0L V12'),

        # 2014
        ('DB9', 2014, '6.0L V12'),
        ('RAPIDE', 2014, '6.0L V12'),
        ('V12 VANTAGE', 2014, '6.0L V12'),
        ('V8 VANTAGE', 2014, '4.7L V8'),
        ('VANQUISH', 2014, '6.0L V12'),

        # 2013
        ('DB9', 2013, '6.0L V12'),
        ('DBS', 2013, '6.0L V12'),
        ('RAPIDE', 2013, '6.0L V12'),
        ('V12 VANTAGE', 2013, '6.0L V12'),
        ('V8 VANTAGE', 2013, '4.7L V8'),
        ('VANQUISH', 2013, '6.0L V12'),

        # 2012
        ('DB9', 2012, '6.0L V12'),
        ('DBS', 2012, '6.0L V12'),
        ('RAPIDE', 2012, '6.0L V12'),
        ('V12 VANTAGE', 2012, '6.0L V12'),
        ('V8 VANTAGE', 2012, '4.7L V8'),
        ('VANQUISH', 2012, '6.0L V12'),

        # 2011
        ('DB9', 2011, '6.0L V12'),
        ('DBS', 2011, '6.0L V12'),
        ('ONE-77', 2011, '7.3L V12'),
        ('RAPIDE', 2011, '6.0L V12'),
        ('V12 VANTAGE', 2011, '6.0L V12'),
        ('V8 VANTAGE', 2011, '4.7L V8'),
        ('VANQUISH', 2011, '6.0L V12'),

        # 2010
        ('DB9', 2010, '6.0L V12'),
        ('DBS', 2010, '6.0L V12'),
        ('RAPIDE', 2010, '6.0L V12'),
        ('V12 VANTAGE', 2010, '6.0L V12'),
        ('V8 VANTAGE', 2010, '4.7L V8'),
        ('VANQUISH', 2010, '6.0L V12'),

        # 2009
        ('DB9', 2009, '6.0L V12'),
        ('DBS', 2009, '6.0L V12'),
        ('V12 VANTAGE', 2009, '6.0L V12'),
        ('V8 VANTAGE', 2009, '4.3L V8'),
        ('V8 VANTAGE', 2009, '4.7L V8'),
        ('VANQUISH', 2009, '6.0L V12'),

        # 2008
        ('DB9', 2008, '6.0L V12'),
        ('DBS', 2008, '6.0L V12'),
        ('V12 VANTAGE', 2008, '6.0L V12'),
        ('V8 VANTAGE', 2008, '4.3L V8'),
        ('V8 VANTAGE', 2008, '4.7L V8'),
        ('VANQUISH', 2008, '6.0L V12'),

        # 2007
        ('DB9', 2007, '6.0L V12'),
        ('V12 VANTAGE', 2007, '6.0L V12'),
        ('V8 VANTAGE', 2007, '4.3L V8'),
        ('VANQUISH', 2007, '6.0L V12'),

        # 2006
        ('DB9', 2006, '6.0L V12'),
        ('V12 VANTAGE', 2006, '6.0L V12'),
        ('V8 VANTAGE', 2006, '4.3L V8'),
        ('VANQUISH', 2006, '6.0L V12'),

        # 2005
        ('DB7', 2005, '5.9L V12'),
        ('DB9', 2005, '6.0L V12'),
        ('VANQUISH', 2005, '6.0L V12'),

        # 2004
        ('DB7', 2004, '5.9L V12'),
        ('DB9', 2004, '6.0L V12'),
        ('VANQUISH', 2004, '6.0L V12'),

        # 2003
        ('DB7', 2003, '5.9L V12'),
        ('DB9', 2003, '6.0L V12'),
        ('VANQUISH', 2003, '6.0L V12'),

        # 2002
        ('DB7', 2002, '5.9L V12'),
        ('VANQUISH', 2002, '6.0L V12'),

        # 2001
        ('DB7', 2001, '5.9L V12'),
        ('VANQUISH', 2001, '6.0L V12'),

        # 2000
        ('DB7', 2000, '3.2L L6 Supercharged'),
        ('DB7', 2000, '5.9L V12'),
        ('VANQUISH', 2000, '6.0L V12'),

        # 1999
        ('DB7', 1999, '3.2L L6 Supercharged'),
        ('DB7', 1999, '5.9L V12'),

        # 1998
        ('DB7', 1998, '3.2L L6 Supercharged'),
        ('DB7', 1998, '5.9L V12'),

        # 1997
        ('DB7', 1997, '3.2L L6 Supercharged'),
        ('DB7', 1997, '5.9L V12'),

        # 1995
        ('VIRAGE', 1995, '5.3L V8'),

        # 1994
        ('VIRAGE', 1994, '5.3L V8'),

        # 1993
        ('VIRAGE', 1993, '5.3L V8'),

        # 1992
        ('VIRAGE', 1992, '5.3L V8'),

        # 1991
        ('VIRAGE', 1991, '5.3L V8'),

        # 1990
        ('VIRAGE', 1990, '5.3L V8'),

        # 1989
        ('LAGONDA', 1989, '5.3L V8'),
        ('VIRAGE', 1989, '5.3L V8'),

        # 1988
        ('LAGONDA', 1988, '5.3L V8'),
        ('VIRAGE', 1988, '5.3L V8'),
        ('ZAGATO', 1988, '5.3L V8'),

        # 1987
        ('LAGONDA', 1987, '5.3L V8'),
        ('VIRAGE', 1987, '5.3L V8'),
        ('ZAGATO', 1987, '5.3L V8'),

        # 1986
        ('LAGONDA', 1986, '5.3L V8'),
        ('VIRAGE', 1986, '5.3L V8'),
        ('ZAGATO', 1986, '5.3L V8'),

        # 1985
        ('LAGONDA', 1985, '5.3L V8'),
        ('VIRAGE', 1985, '5.3L V8'),
        ('ZAGATO', 1985, '5.3L V8'),

        # 1984
        ('LAGONDA', 1984, '5.3L V8'),
        ('VIRAGE', 1984, '5.3L V8'),
        ('ZAGATO', 1984, '5.3L V8'),

        # 1983
        ('LAGONDA', 1983, '5.3L V8'),
        ('VIRAGE', 1983, '5.3L V8'),
        ('ZAGATO', 1983, '5.3L V8'),

        # 1982
        ('LAGONDA', 1982, '5.3L V8'),
        ('VIRAGE', 1982, '5.3L V8'),
        ('ZAGATO', 1982, '5.3L V8'),

        # 1981
        ('LAGONDA', 1981, '5.3L V8'),
        ('VIRAGE', 1981, '5.3L V8'),
        ('ZAGATO', 1981, '5.3L V8'),

        # 1980
        ('LAGONDA', 1980, '5.3L V8'),
        ('VIRAGE', 1980, '5.3L V8'),
        ('ZAGATO', 1980, '5.3L V8'),

        # 1979
        ('LAGONDA', 1979, '5.3L V8'),
        ('VIRAGE', 1979, '5.3L V8'),
        ('ZAGATO', 1979, '5.3L V8'),

        # 1978
        ('LAGONDA', 1978, '5.3L V8'),
        ('VIRAGE', 1978, '5.3L V8'),
        ('ZAGATO', 1978, '5.3L V8'),

        # 1977
        ('LAGONDA', 1977, '5.3L V8'),
        ('VIRAGE', 1977, '5.3L V8'),
        ('ZAGATO', 1977, '5.3L V8'),

        # 1976
        ('LAGONDA', 1976, '5.3L V8'),
        ('VIRAGE', 1976, '5.3L V8'),
        ('ZAGATO', 1976, '5.3L V8'),

        # 1975
        ('LAGONDA', 1975, '5.3L V8'),
        ('VIRAGE', 1975, '5.3L V8'),
        ('ZAGATO', 1975, '5.3L V8'),

        # ARRA (modelo italiano)
        ('ARRA', 2024, '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 ASTON MARTIN vehicles")
    
    # Close connection
    conn.close()
    print("Aston Martin data added successfully!")


if __name__ == "__main__":
    main()