#!/usr/bin/env python3
"""
Script para agregar datos de Acura a la base de datos de vehículos
"""

import sqlite3
import os

def add_acura_data():
    # Verificar que la base de datos exista
    db_path = "vehicle_database/vehicle_database.db"
    if not os.path.exists(db_path):
        print(f"Error: Base de datos no encontrada en {db_path}")
        return

    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    print("Agregando datos de Acura a la base de datos...")

    try:
        # Insertar la marca Acura
        cursor.execute("INSERT OR IGNORE INTO brands (name) VALUES (?)", ("ACURA",))
        cursor.execute("SELECT id FROM brands WHERE name = ?", ("ACURA",))
        brand_id = cursor.fetchone()[0]
        print(f"Marca ACURA tiene ID: {brand_id}")

        # Insertar modelos de Acura
        models = [
            'MDX', 'ADX', 'INTEGRA', 'RDX', 'TLX', 'ZDX', 'ILX', 'NSX', 'RLX', 'TL', 'TSX', 'CSX'
        ]

        for model in models:
            cursor.execute(
                "INSERT OR IGNORE INTO models (brand_id, name) VALUES (?, ?)",
                (brand_id, model)
            )
        print(f"Agregados {len(models)} modelos de Acura")

        # Datos de años y motores para cada modelo
        acura_data = [
            # 2026
            ('MDX', 2026, '3.0L V6 Turbocharged'),
            ('MDX', 2026, '3.5L V6'),

            # 2025
            ('ADX', 2025, '1.5L L4 Turbocharged'),
            ('INTEGRA', 2025, '1.5L L4 Turbocharged'),
            ('INTEGRA', 2025, '2.0L L4 Turbocharged'),
            ('MDX', 2025, '3.0L V6 Turbocharged'),
            ('MDX', 2025, '3.5L V6'),
            ('RDX', 2025, '2.0L L4 Turbocharged'),
            ('TLX', 2025, '2.0L L4 Turbocharged'),
            ('TLX', 2025, '3.0L V6 Turbocharged'),

            # 2024
            ('INTEGRA', 2024, '1.5L L4 Turbocharged'),
            ('INTEGRA', 2024, '2.0L L4 Turbocharged'),
            ('MDX', 2024, '3.0L V6 Turbocharged'),
            ('MDX', 2024, '3.5L V6'),
            ('RDX', 2024, '2.0L L4 Turbocharged'),
            ('TLX', 2024, '2.0L L4 Turbocharged'),
            ('TLX', 2024, '3.0L V6 Turbocharged'),
            ('ZDX', 2024, 'ELECTRIC'),

            # 2023
            ('INTEGRA', 2023, '1.5L L4 Turbocharged'),
            ('MDX', 2023, '3.0L V6 Turbocharged'),
            ('MDX', 2023, '3.5L V6'),
            ('RDX', 2023, '2.0L L4 Turbocharged'),
            ('TLX', 2023, '2.0L L4 Turbocharged'),
            ('TLX', 2023, '3.0L V6 Turbocharged'),

            # 2022
            ('ILX', 2022, '2.4L L4'),
            ('MDX', 2022, '3.0L V6 Turbocharged'),
            ('MDX', 2022, '3.5L V6'),
            ('NSX', 2022, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2022, '2.0L L4 Turbocharged'),
            ('TLX', 2022, '2.0L L4 Turbocharged'),
            ('TLX', 2022, '3.0L V6 Turbocharged'),

            # 2021
            ('ILX', 2021, '2.4L L4'),
            ('NSX', 2021, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2021, '2.0L L4 Turbocharged'),
            ('TLX', 2021, '2.0L L4 Turbocharged'),
            ('TLX', 2021, '3.0L V6 Turbocharged'),

            # 2020
            ('ILX', 2020, '2.4L L4'),
            ('MDX', 2020, '3.0L V6 ELECTRIC/GAS'),
            ('MDX', 2020, '3.5L V6'),
            ('NSX', 2020, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2020, '2.0L L4 Turbocharged'),
            ('RLX', 2020, '3.5L V6'),
            ('RLX', 2020, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2020, '2.4L L4'),
            ('TLX', 2020, '3.5L V6 DOHC'),
            ('TLX', 2020, '3.5L V6 SOHC'),

            # 2019
            ('ILX', 2019, '2.4L L4'),
            ('MDX', 2019, '3.0L V6'),
            ('MDX', 2019, '3.0L V6 ELECTRIC/GAS'),
            ('MDX', 2019, '3.5L V6'),
            ('NSX', 2019, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2019, '2.0L L4 Turbocharged'),
            ('RLX', 2019, '3.5L V6'),
            ('RLX', 2019, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2019, '2.4L L4'),
            ('TLX', 2019, '3.5L V6'),

            # 2018
            ('ILX', 2018, '2.4L L4'),
            ('MDX', 2018, '3.0L V6 ELECTRIC/GAS'),
            ('MDX', 2018, '3.5L V6'),
            ('NSX', 2018, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2018, '3.5L V6'),
            ('RLX', 2018, '3.5L V6'),
            ('RLX', 2018, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2018, '2.4L L4'),
            ('TLX', 2018, '3.5L V6'),

            # 2017
            ('ILX', 2017, '2.4L L4'),
            ('MDX', 2017, '3.0L V6 ELECTRIC/GAS'),
            ('MDX', 2017, '3.5L V6'),
            ('NSX', 2017, '3.5L V6 ELECTRIC/GAS Turbocharged'),
            ('RDX', 2017, '3.5L V6'),
            ('RLX', 2017, '3.5L V6'),
            ('RLX', 2017, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2017, '2.4L L4'),
            ('TLX', 2017, '3.5L V6'),

            # 2016
            ('ILX', 2016, '2.4L L4'),
            ('MDX', 2016, '3.5L V6'),
            ('RDX', 2016, '3.5L V6'),
            ('RLX', 2016, '3.5L V6'),
            ('RLX', 2016, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2016, '2.4L L4'),
            ('TLX', 2016, '3.5L V6'),

            # 2015
            ('ILX', 2015, '1.5L L4 ELECTRIC/GAS'),
            ('ILX', 2015, '2.0L L4'),
            ('ILX', 2015, '2.4L L4'),
            ('MDX', 2015, '3.5L V6'),
            ('RDX', 2015, '3.5L V6'),
            ('RLX', 2015, '3.5L V6'),
            ('RLX', 2015, '3.5L V6 ELECTRIC/GAS'),
            ('TLX', 2015, '2.4L L4'),
            ('TLX', 2015, '3.5L V6'),

            # 2014
            ('ILX', 2014, '1.5L L4 ELECTRIC/GAS'),
            ('ILX', 2014, '2.0L L4'),
            ('ILX', 2014, '2.4L L4'),
            ('MDX', 2014, '3.5L V6'),
            ('RDX', 2014, '3.5L V6'),
            ('RLX', 2014, '3.5L V6'),
            ('RLX', 2014, '3.5L V6 ELECTRIC/GAS'),
            ('TL', 2014, '3.5L V6'),
            ('TSX', 2014, '2.4L L4'),
            ('TSX', 2014, '3.5L V6'),

            # 2013
            ('ILX', 2013, '1.5L L4 ELECTRIC/GAS'),
            ('ILX', 2013, '2.0L L4'),
            ('ILX', 2013, '2.4L L4'),
            ('MDX', 2013, '3.7L V6'),
            ('RDX', 2013, '3.5L V6'),
            ('TL', 2013, '3.5L V6'),
            ('TL', 2013, '3.7L V6'),
            ('TSX', 2013, '2.4L L4'),
            ('TSX', 2013, '3.5L V6'),
            ('ZDX', 2013, '3.7L V6'),

            # 2012
            ('MDX', 2012, '3.7L V6'),
            ('RDX', 2012, '2.3L L4 Turbocharged'),
            ('RL', 2012, '3.7L V6'),
            ('TL', 2012, '3.5L V6'),
            ('TL', 2012, '3.7L V6'),
            ('TSX', 2012, '2.4L L4'),
            ('TSX', 2012, '3.5L V6'),
            ('ZDX', 2012, '3.7L V6'),

            # 2011
            ('CSX', 2011, '2.0L L4'),
            ('MDX', 2011, '3.7L V6'),
            ('RDX', 2011, '2.3L L4 Turbocharged'),
            ('RL', 2011, '3.7L V6'),
            ('TL', 2011, '3.5L V6'),
            ('TL', 2011, '3.7L V6'),
            ('TSX', 2011, '2.4L L4'),
            ('TSX', 2011, '3.5L V6'),
            ('ZDX', 2011, '3.7L V6'),

            # 2010
            ('CSX', 2010, '2.0L L4'),
            ('MDX', 2010, '3.7L V6'),
            ('RDX', 2010, '2.3L L4 Turbocharged'),
            ('RL', 2010, '3.7L V6'),
            ('TL', 2010, '3.5L V6'),
            ('TL', 2010, '3.7L V6'),
            ('TSX', 2010, '2.4L L4'),
            ('TSX', 2010, '3.5L V6'),
            ('ZDX', 2010, '3.7L V6'),

            # 2009
            ('CSX', 2009, '2.0L L4'),
            ('MDX', 2009, '3.7L V6'),
            ('RDX', 2009, '2.3L L4 Turbocharged'),
            ('RL', 2009, '3.7L V6'),
            ('TL', 2009, '3.5L V6'),
            ('TL', 2009, '3.7L V6'),
            ('TSX', 2009, '2.4L L4'),
            ('TSX', 2009, '3.5L V6'),

            # 2008
            ('CSX', 2008, '2.0L L4'),
            ('MDX', 2008, '3.7L V6'),
            ('RDX', 2008, '2.3L L4 Turbocharged'),
            ('RL', 2008, '3.5L V6'),
            ('TL', 2008, '3.2L V6'),
            ('TL', 2008, '3.5L V6'),
            ('TSX', 2008, '2.4L L4'),
            ('TSX', 2008, '3.5L V6'),

            # 2007
            ('CSX', 2007, '2.0L L4'),
            ('MDX', 2007, '3.7L V6'),
            ('RDX', 2007, '2.3L L4 Turbocharged'),
            ('RL', 2007, '3.5L V6'),
            ('TL', 2007, '3.2L V6'),
            ('TL', 2007, '3.5L V6'),
            ('TSX', 2007, '2.4L L4')
        ]

        # Insertar años
        years = list(set([data[1] for data in acura_data]))  # Obtener años únicos
        for year in years:
            cursor.execute("INSERT OR IGNORE INTO years (year) VALUES (?)", (year,))
        print(f"Agregados {len(years)} años")

        # Insertar motores
        engines = list(set([data[2] for data in acura_data]))  # Obtener motores únicos
        for engine in engines:
            cursor.execute("INSERT OR IGNORE INTO engines (name) VALUES (?)", (engine,))
        print(f"Agregados {len(engines)} motores")

        # Crear combinaciones modelo-año-motor
        for model_name, year, engine_name in acura_data:
            # Obtener IDs
            cursor.execute("SELECT id FROM models WHERE brand_id = ? AND name = ?", (brand_id, model_name))
            model_result = cursor.fetchone()
            if model_result:
                model_id = model_result[0]
            else:
                print(f"Advertencia: Modelo {model_name} no encontrado para la marca ACURA")
                continue

            cursor.execute("SELECT id FROM years WHERE year = ?", (year,))
            year_result = cursor.fetchone()
            if year_result:
                year_id = year_result[0]
            else:
                print(f"Advertencia: Año {year} no encontrado")
                continue

            cursor.execute("SELECT id FROM engines WHERE name = ?", (engine_name,))
            engine_result = cursor.fetchone()
            if engine_result:
                engine_id = engine_result[0]
            else:
                print(f"Advertencia: Motor {engine_name} no encontrado")
                continue

            # Insertar la combinación modelo-año-motor
            cursor.execute(
                """INSERT OR REPLACE INTO model_year_engine
                   (model_id, year_id, engine_id) VALUES (?, ?, ?)""",
                (model_id, year_id, engine_id)
            )

        conn.commit()
        print(f"Se agregaron {len(acura_data)} combinaciones modelo-año-motor para ACURA")
        print("Datos de ACURA agregados exitosamente a la base de datos!")

        # Mostrar algunos resultados
        print("\nAlgunos vehículos ACURA agregados:")
        cursor.execute("""
            SELECT b.name, m.name, y.year, e.name
            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
            WHERE b.name = 'ACURA'
            ORDER BY m.name, y.year DESC
            LIMIT 15
        """)
        
        results = cursor.fetchall()
        for brand, model, year, engine in results:
            print(f"  {year} {brand} {model} - {engine}")

    except Exception as e:
        print(f"Error al agregar datos de ACURA: {e}")
        conn.rollback()
    finally:
        conn.close()

if __name__ == "__main__":
    add_acura_data()