#!/usr/bin/env python3
"""
Script para poblar la base de datos con datos de demo realistas.
Incluye partes OEM, fabricantes aftermarket, partes alternativas y fitments.
"""

import sqlite3
from pathlib import Path

DB_PATH = Path(__file__).parent.parent / 'vehicle_database.db'

# Demo Parts Data - Partes realistas comunes
DEMO_PARTS = [
    # Brake System (category 2, group varies)
    {'oem': '04465-33450', 'name': 'Front Brake Pad Set', 'name_es': 'Juego de Pastillas Delanteras', 'group': 'Brake Pads/Shoes', 'desc': 'Ceramic front brake pads', 'weight': 1.2},
    {'oem': '04466-33180', 'name': 'Rear Brake Pad Set', 'name_es': 'Juego de Pastillas Traseras', 'group': 'Brake Pads/Shoes', 'desc': 'Ceramic rear brake pads', 'weight': 0.9},
    {'oem': '43512-33130', 'name': 'Front Brake Rotor', 'name_es': 'Disco de Freno Delantero', 'group': 'Brake Rotors', 'desc': 'Vented front brake disc', 'weight': 8.5},
    {'oem': '42431-33130', 'name': 'Rear Brake Rotor', 'name_es': 'Disco de Freno Trasero', 'group': 'Brake Rotors', 'desc': 'Solid rear brake disc', 'weight': 5.2},
    {'oem': '47750-33210', 'name': 'Brake Caliper Front Right', 'name_es': 'Caliper Delantero Derecho', 'group': 'Brake Calipers', 'desc': 'Front right brake caliper assembly', 'weight': 4.5},
    {'oem': '47730-33210', 'name': 'Brake Caliper Front Left', 'name_es': 'Caliper Delantero Izquierdo', 'group': 'Brake Calipers', 'desc': 'Front left brake caliper assembly', 'weight': 4.5},

    # Engine (category 6)
    {'oem': '90915-YZZD4', 'name': 'Oil Filter', 'name_es': 'Filtro de Aceite', 'group': 'Oil Filters', 'desc': 'Spin-on oil filter element', 'weight': 0.3},
    {'oem': '17801-0P010', 'name': 'Air Filter', 'name_es': 'Filtro de Aire', 'group': 'Air Filters', 'desc': 'Engine air filter element', 'weight': 0.25},
    {'oem': '23300-79525', 'name': 'Fuel Filter', 'name_es': 'Filtro de Combustible', 'group': 'Fuel Filters', 'desc': 'In-line fuel filter', 'weight': 0.4},
    {'oem': '90048-51003', 'name': 'Spark Plug Iridium', 'name_es': 'Bujia de Iridio', 'group': 'Spark Plugs', 'desc': 'Long-life iridium spark plug', 'weight': 0.05},
    {'oem': '13568-09130', 'name': 'Timing Belt', 'name_es': 'Banda de Tiempo', 'group': 'Timing Components', 'desc': 'Engine timing belt', 'weight': 0.35},
    {'oem': 'SU003-02574', 'name': 'Serpentine Belt', 'name_es': 'Banda Serpentina', 'group': 'Belts', 'desc': 'Multi-rib accessory drive belt', 'weight': 0.2},
    {'oem': '16100-09515', 'name': 'Water Pump', 'name_es': 'Bomba de Agua', 'group': 'Water Pumps', 'desc': 'Engine coolant water pump', 'weight': 2.1},
    {'oem': '16363-0P030', 'name': 'Thermostat', 'name_es': 'Termostato', 'group': 'Thermostats', 'desc': 'Engine thermostat with housing', 'weight': 0.3},

    # Suspension (category 11)
    {'oem': '48157-42010', 'name': 'Front Strut Mount', 'name_es': 'Soporte de Amortiguador Delantero', 'group': 'Strut Mounts', 'desc': 'Front suspension strut mount', 'weight': 0.8},
    {'oem': '48609-48020', 'name': 'Front Strut Assembly', 'name_es': 'Amortiguador Delantero Completo', 'group': 'Struts/Shocks', 'desc': 'Complete front strut assembly', 'weight': 6.5},
    {'oem': '48530-09S00', 'name': 'Rear Shock Absorber', 'name_es': 'Amortiguador Trasero', 'group': 'Struts/Shocks', 'desc': 'Rear shock absorber', 'weight': 3.2},
    {'oem': '48068-33070', 'name': 'Front Lower Control Arm', 'name_es': 'Brazo de Control Inferior Delantero', 'group': 'Control Arms', 'desc': 'Front lower control arm with bushing', 'weight': 4.8},
    {'oem': '48725-33050', 'name': 'Rear Trailing Arm', 'name_es': 'Brazo Trasero', 'group': 'Control Arms', 'desc': 'Rear suspension trailing arm', 'weight': 3.5},
    {'oem': '48815-33070', 'name': 'Sway Bar Link Front', 'name_es': 'Enlace de Barra Estabilizadora', 'group': 'Sway Bar Links', 'desc': 'Front stabilizer bar end link', 'weight': 0.4},

    # Steering (category 10)
    {'oem': '45046-39335', 'name': 'Outer Tie Rod End', 'name_es': 'Terminal Exterior de Direccion', 'group': 'Tie Rods', 'desc': 'Outer steering tie rod end', 'weight': 0.6},
    {'oem': '45503-39165', 'name': 'Inner Tie Rod', 'name_es': 'Terminal Interior de Direccion', 'group': 'Tie Rods', 'desc': 'Inner steering tie rod', 'weight': 0.5},
    {'oem': '44200-33480', 'name': 'Power Steering Pump', 'name_es': 'Bomba de Direccion', 'group': 'Power Steering', 'desc': 'Hydraulic power steering pump', 'weight': 3.8},
    {'oem': '45510-33310', 'name': 'Steering Rack', 'name_es': 'Cremallera de Direccion', 'group': 'Steering Racks', 'desc': 'Power steering rack and pinion', 'weight': 12.5},

    # Electrical (category 5)
    {'oem': '28100-21030', 'name': 'Starter Motor', 'name_es': 'Motor de Arranque', 'group': 'Starters', 'desc': 'Engine starter motor', 'weight': 4.2},
    {'oem': '27060-37030', 'name': 'Alternator', 'name_es': 'Alternador', 'group': 'Alternators', 'desc': '100 amp alternator', 'weight': 5.8},
    {'oem': '90919-02240', 'name': 'Ignition Coil', 'name_es': 'Bobina de Ignicion', 'group': 'Ignition Coils', 'desc': 'Direct ignition coil', 'weight': 0.35},
    {'oem': '89467-33040', 'name': 'Oxygen Sensor', 'name_es': 'Sensor de Oxigeno', 'group': 'Sensors', 'desc': 'Upstream O2 sensor', 'weight': 0.15},

    # Cooling System (category 3)
    {'oem': '16400-0W010', 'name': 'Radiator', 'name_es': 'Radiador', 'group': 'Radiators', 'desc': 'Engine cooling radiator', 'weight': 8.5},
    {'oem': '16711-31310', 'name': 'Radiator Fan', 'name_es': 'Ventilador de Radiador', 'group': 'Cooling Fans', 'desc': 'Electric radiator cooling fan', 'weight': 3.2},
    {'oem': '16361-0P030', 'name': 'Radiator Hose Upper', 'name_es': 'Manguera Superior de Radiador', 'group': 'Hoses', 'desc': 'Upper radiator coolant hose', 'weight': 0.4},

    # Exhaust (category 7)
    {'oem': '17140-31620', 'name': 'Catalytic Converter', 'name_es': 'Convertidor Catalitico', 'group': 'Catalytic Converters', 'desc': 'Three-way catalytic converter', 'weight': 6.5},
    {'oem': '17430-31410', 'name': 'Muffler', 'name_es': 'Silenciador', 'group': 'Mufflers', 'desc': 'Rear exhaust muffler', 'weight': 8.2},

    # Fuel System (category 8)
    {'oem': '23220-31100', 'name': 'Fuel Pump', 'name_es': 'Bomba de Combustible', 'group': 'Fuel Pumps', 'desc': 'Electric in-tank fuel pump', 'weight': 0.8},
    {'oem': '23250-31010', 'name': 'Fuel Injector', 'name_es': 'Inyector de Combustible', 'group': 'Fuel Injectors', 'desc': 'Multi-port fuel injector', 'weight': 0.1},

    # Drivetrain (category 4)
    {'oem': '31470-52011', 'name': 'Clutch Kit', 'name_es': 'Kit de Embrague', 'group': 'Clutches', 'desc': 'Complete clutch replacement kit', 'weight': 7.5},
    {'oem': '43502-35210', 'name': 'CV Axle Front', 'name_es': 'Flecha Homocinética Delantera', 'group': 'CV Axles', 'desc': 'Front CV axle shaft assembly', 'weight': 5.2},

    # HVAC (category 9)
    {'oem': '88310-33250', 'name': 'AC Compressor', 'name_es': 'Compresor de AC', 'group': 'AC Compressors', 'desc': 'Air conditioning compressor', 'weight': 6.8},
    {'oem': '87103-33110', 'name': 'Blower Motor', 'name_es': 'Motor de Ventilador', 'group': 'Blower Motors', 'desc': 'HVAC blower motor', 'weight': 1.5},
    {'oem': '87139-YZZ05', 'name': 'Cabin Air Filter', 'name_es': 'Filtro de Cabina', 'group': 'Cabin Filters', 'desc': 'Cabin air filter element', 'weight': 0.15},
]

# Demo Manufacturers
DEMO_MANUFACTURERS = [
    {'name': 'Brembo', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Italy'},
    {'name': 'Bosch', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Germany'},
    {'name': 'Denso', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Japan'},
    {'name': 'ACDelco', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Raybestos', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Wagner', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Monroe', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'KYB', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Japan'},
    {'name': 'NGK', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Japan'},
    {'name': 'Aisin', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Japan'},
    {'name': 'Gates', 'type': 'aftermarket', 'tier': 'premium', 'country': 'USA'},
    {'name': 'Continental', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Germany'},
    {'name': 'Moog', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Dorman', 'type': 'aftermarket', 'tier': 'economy', 'country': 'USA'},
    {'name': 'Centric', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Beck/Arnley', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Cardone', 'type': 'remanufactured', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Standard Motor', 'type': 'aftermarket', 'tier': 'standard', 'country': 'USA'},
    {'name': 'Spectra Premium', 'type': 'aftermarket', 'tier': 'economy', 'country': 'Canada'},
    {'name': 'TRW', 'type': 'aftermarket', 'tier': 'premium', 'country': 'Germany'},
]


def get_group_id(cursor, group_name):
    """Get or create a group by name"""
    cursor.execute("SELECT id FROM part_groups WHERE name = ?", (group_name,))
    row = cursor.fetchone()
    if row:
        return row[0]
    return None


def populate_parts(cursor):
    """Populate OEM parts"""
    print("Adding OEM parts...")
    added = 0
    for part in DEMO_PARTS:
        group_id = get_group_id(cursor, part['group'])
        if not group_id:
            print(f"  Warning: Group '{part['group']}' not found, skipping {part['oem']}")
            continue

        # Check if part already exists
        cursor.execute("SELECT id FROM parts WHERE oem_part_number = ?", (part['oem'],))
        if cursor.fetchone():
            continue

        cursor.execute("""
            INSERT INTO parts (oem_part_number, name, name_es, group_id, description, weight_kg)
            VALUES (?, ?, ?, ?, ?, ?)
        """, (part['oem'], part['name'], part['name_es'], group_id, part['desc'], part['weight']))
        added += 1

    print(f"  Added {added} OEM parts")
    return added


def populate_manufacturers(cursor):
    """Populate manufacturers"""
    print("Adding manufacturers...")
    added = 0
    for mfr in DEMO_MANUFACTURERS:
        cursor.execute("SELECT id FROM manufacturers WHERE name = ?", (mfr['name'],))
        if cursor.fetchone():
            continue

        cursor.execute("""
            INSERT INTO manufacturers (name, type, quality_tier, country)
            VALUES (?, ?, ?, ?)
        """, (mfr['name'], mfr['type'], mfr['tier'], mfr['country']))
        added += 1

    print(f"  Added {added} manufacturers")
    return added


def populate_aftermarket(cursor):
    """Populate aftermarket parts linked to OEM"""
    print("Adding aftermarket alternatives...")

    # Get all parts and manufacturers
    cursor.execute("SELECT id, oem_part_number, name FROM parts")
    parts = cursor.fetchall()

    cursor.execute("SELECT id, name, quality_tier FROM manufacturers WHERE type != 'oem'")
    manufacturers = cursor.fetchall()

    added = 0
    import random

    for part in parts:
        part_id, oem_num, part_name = part

        # Add 2-4 aftermarket alternatives per OEM part
        num_alternatives = random.randint(2, 4)
        selected_mfrs = random.sample(manufacturers, min(num_alternatives, len(manufacturers)))

        for mfr in selected_mfrs:
            mfr_id, mfr_name, tier = mfr

            # Check if already exists
            cursor.execute("""
                SELECT id FROM aftermarket_parts WHERE oem_part_id = ? AND manufacturer_id = ?
            """, (part_id, mfr_id))
            if cursor.fetchone():
                continue

            # Generate part number
            prefix = mfr_name[:3].upper()
            part_num = f"{prefix}-{oem_num.replace('-', '')[:6]}"

            # Set price based on tier
            base_price = random.uniform(20, 200)
            if tier == 'premium':
                price = base_price * 1.5
                warranty = 36
            elif tier == 'economy':
                price = base_price * 0.6
                warranty = 12
            else:
                price = base_price
                warranty = 24

            cursor.execute("""
                INSERT INTO aftermarket_parts (oem_part_id, manufacturer_id, part_number, name, quality_tier, price_usd, warranty_months)
                VALUES (?, ?, ?, ?, ?, ?, ?)
            """, (part_id, mfr_id, part_num, part_name, tier, round(price, 2), warranty))
            added += 1

    print(f"  Added {added} aftermarket parts")
    return added


def populate_fitments(cursor):
    """Create fitments linking parts to vehicles"""
    print("Adding fitments (linking parts to vehicles)...")

    # Get some vehicle configurations (first 50)
    cursor.execute("""
        SELECT mye.id, b.name as brand, m.name as model, y.year
        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
        WHERE b.name IN ('TOYOTA', 'HONDA', 'FORD', 'CHEVROLET', 'NISSAN')
        ORDER BY RANDOM()
        LIMIT 100
    """)
    vehicles = cursor.fetchall()

    # Get all parts
    cursor.execute("SELECT id, name FROM parts")
    parts = cursor.fetchall()

    added = 0
    import random

    for vehicle in vehicles:
        mye_id = vehicle[0]

        # Assign 10-20 random parts to each vehicle
        num_parts = random.randint(10, 20)
        selected_parts = random.sample(parts, min(num_parts, len(parts)))

        for part in selected_parts:
            part_id = part[0]

            # Check if fitment already exists
            cursor.execute("""
                SELECT id FROM vehicle_parts WHERE model_year_engine_id = ? AND part_id = ?
            """, (mye_id, part_id))
            if cursor.fetchone():
                continue

            # Determine position based on part name
            part_name = part[1].lower()
            position = None
            quantity = 1

            if 'front' in part_name:
                position = 'front'
            elif 'rear' in part_name:
                position = 'rear'
            elif 'left' in part_name:
                position = 'left'
            elif 'right' in part_name:
                position = 'right'

            # Some parts need multiples
            if 'spark plug' in part_name or 'injector' in part_name:
                quantity = 4
            elif 'pad' in part_name or 'rotor' in part_name:
                quantity = 2 if position else 1

            cursor.execute("""
                INSERT INTO vehicle_parts (model_year_engine_id, part_id, quantity_required, position)
                VALUES (?, ?, ?, ?)
            """, (mye_id, part_id, quantity, position))
            added += 1

    print(f"  Added {added} fitments")
    return added


def main():
    print("=" * 50)
    print("Populating Demo Data")
    print("=" * 50)

    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    try:
        populate_parts(cursor)
        populate_manufacturers(cursor)
        populate_aftermarket(cursor)
        populate_fitments(cursor)

        conn.commit()
        print("\nDemo data populated successfully!")

        # Show final counts
        print("\n=== Final Counts ===")
        for table in ['parts', 'manufacturers', 'aftermarket_parts', 'vehicle_parts']:
            cursor.execute(f"SELECT COUNT(*) FROM {table}")
            count = cursor.fetchone()[0]
            print(f"  {table}: {count}")

    except Exception as e:
        conn.rollback()
        print(f"Error: {e}")
        raise
    finally:
        conn.close()


if __name__ == '__main__':
    main()
