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

import sqlite3
import os

def add_abarth_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 Abarth a la base de datos...")
    
    try:
        # Insertar la marca Abarth
        cursor.execute("INSERT OR IGNORE INTO brands (name) VALUES (?)", ("Abarth",))
        cursor.execute("SELECT id FROM brands WHERE name = ?", ("Abarth",))
        brand_id = cursor.fetchone()[0]
        print(f"Marca Abarth tiene ID: {brand_id}")
        
        # Insertar modelos de Abarth
        models = [
            '1000', '1300', '2000', 'SIMCA', '850', '1150', '1600', '750', '204', '205'
        ]
        
        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 Abarth")
        
        # Datos de años y motores para cada modelo
        abarth_data = [
            # 1000
            ('1000', 1969, '982cc L4'),
            ('1000', 1968, '982cc L4'),
            ('1000', 1967, '982cc L4'),
            ('1000', 1966, '982cc L4'),
            ('1000', 1965, '982cc L4'),
            ('1000', 1964, '982cc L4'),
            ('1000', 1963, '982cc L4'),
            ('1000', 1962, '982cc L4'),
            
            # 1300
            ('1300', 1969, '1.3L L4'),
            ('1300', 1968, '1.3L L4'),
            ('1300', 1967, '1.3L L4'),
            ('1300', 1966, '1.3L L4'),
            
            # 2000
            ('2000', 1969, '1.9L L4'),
            ('2000', 1968, '1.9L L4'),
            ('2000', 1967, '1.9L L4'),
            ('2000', 1966, '1.9L L4'),
            
            # SIMCA
            ('SIMCA', 1969, '1.3L L4'),
            ('SIMCA', 1969, '2.0L L4'),
            ('SIMCA', 1968, '1.3L L4'),
            ('SIMCA', 1968, '2.0L L4'),
            ('SIMCA', 1967, '1.3L L4'),
            ('SIMCA', 1967, '2.0L L4'),
            ('SIMCA', 1966, '1.3L L4'),
            ('SIMCA', 1966, '2.0L L4'),
            ('SIMCA', 1965, '1.3L L4'),
            ('SIMCA', 1965, '2.0L L4'),
            ('SIMCA', 1964, '1.3L L4'),
            ('SIMCA', 1964, '2.0L L4'),
            ('SIMCA', 1963, '1.3L L4'),
            ('SIMCA', 1963, '2.0L L4'),
            ('SIMCA', 1962, '1.3L L4'),
            ('SIMCA', 1962, '2.0L L4'),
            
            # 850
            ('850', 1966, '847cc L4'),
            ('850', 1965, '847cc L4'),
            ('850', 1964, '847cc L4'),
            ('850', 1963, '847cc L4'),
            ('850', 1962, '847cc L4'),
            ('850', 1961, '833cc L4'),
            ('850', 1960, '833cc L4'),
            
            # 1150
            ('1150', 1966, '1.1L L4'),
            
            # 1600
            ('1600', 1966, '1.6L L4'),
            ('1600', 1965, '1.6L L4'),
            ('1600', 1964, '1.6L L4'),
            ('1600', 1963, '1.6L L4'),
            ('1600', 1962, '1.6L L4'),
            ('1600', 1961, '1.6L L4'),
            ('1600', 1960, '1.6L L4'),
            
            # 750
            ('750', 1961, '747cc L4'),
            ('750', 1960, '747cc L4'),
            ('750', 1959, '747cc L4'),
            ('750', 1958, '747cc L4'),
            ('750', 1957, '747cc L4'),
            ('750', 1956, '747cc L4'),
            
            # 204
            ('204', 1955, '1.1L L4'),
            ('204', 1954, '1.1L L4'),
            ('204', 1953, '1.1L L4'),
            ('204', 1952, '1.1L L4'),
            ('204', 1951, '1.1L L4'),
            ('204', 1950, '1.1L L4'),
            ('204', 1949, '1.1L L4'),
            
            # 205
            ('205', 1955, '1.1L L4'),
            ('205', 1954, '1.1L L4'),
            ('205', 1953, '1.1L L4'),
            ('205', 1952, '1.1L L4'),
            ('205', 1951, '1.1L L4'),
            ('205', 1950, '1.1L L4'),
            ('205', 1949, '1.1L L4'),
        ]
        
        # Insertar años
        years = list(set([data[1] for data in abarth_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 abarth_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 abarth_data:
            # Obtener IDs
            cursor.execute("SELECT id FROM models WHERE brand_id = ? AND name = ?", (brand_id, model_name))
            model_id = cursor.fetchone()
            if model_id:
                model_id = model_id[0]
            else:
                print(f"Advertencia: Modelo {model_name} no encontrado para la marca Abarth")
                continue
                
            cursor.execute("SELECT id FROM years WHERE year = ?", (year,))
            year_id = cursor.fetchone()[0]
            
            cursor.execute("SELECT id FROM engines WHERE name = ?", (engine_name,))
            engine_id = cursor.fetchone()[0]
            
            # Insertar la combinación modelo-año-motor
            cursor.execute(
                """INSERT OR IGNORE INTO model_year_engine 
                   (model_id, year_id, engine_id) VALUES (?, ?, ?)""",
                (model_id, year_id, engine_id)
            )
        
        conn.commit()
        print(f"Se agregaron {len(abarth_data)} combinaciones modelo-año-motor para Abarth")
        print("Datos de Abarth agregados exitosamente a la base de datos!")
        
    except Exception as e:
        print(f"Error al agregar datos de Abarth: {e}")
        conn.rollback()
    finally:
        conn.close()

if __name__ == "__main__":
    add_abarth_data()