import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from dotenv import load_dotenv

load_dotenv(os.path.join(os.path.dirname(__file__), '../../.env'))

DATABASE_URL = os.getenv("DATABASE_URL")

# Kezdő adatkészlet
INITIAL_DATA = {
    "BMW": ["3 Series", "5 Series", "X5", "R 1250 GS (Motor)"],
    "Toyota": ["Corolla", "Yaris", "RAV4", "Hilux"],
    "Volkswagen": ["Golf", "Passat", "Tiguan", "Polo"],
    "Honda": ["Civic", "CR-V", "CB500X (Motor)", "Africa Twin (Motor)"],
    "Audi": ["A3", "A4", "A6", "Q5"],
    "Mercedes-Benz": ["C-Class", "E-Class", "S-Class", "G-Class"],
    "Suzuki": ["Swift", "Vitara", "S-Cross", "DL 650 V-Strom (Motor)"]
}

async def seed_vehicles():
    # Ha a DATABASE_URL környezeti változóból jön, azt használjuk
    engine = create_async_engine(DATABASE_URL)
    
    try:
        async with engine.begin() as conn:
            print("--- 🛠️  Struktúra ellenőrzése és építése... ---")
            
            # 1. SÉMA ÉS TÁBLÁK LÉTREHOZÁSA (Ez a hiányzó láncszem!)
            await conn.execute(text("CREATE SCHEMA IF NOT EXISTS ref;"))
            
            await conn.execute(text("""
                CREATE TABLE IF NOT EXISTS ref.vehicle_makes (
                    id SERIAL PRIMARY KEY,
                    name VARCHAR(100) UNIQUE NOT NULL
                );
            """))
            
            await conn.execute(text("""
                CREATE TABLE IF NOT EXISTS ref.vehicle_models (
                    id SERIAL PRIMARY KEY,
                    make_id INTEGER REFERENCES ref.vehicle_makes(id) ON DELETE CASCADE,
                    model_name VARCHAR(100) NOT NULL,
                    category VARCHAR(50),
                    technical_data JSONB,
                    UNIQUE(make_id, model_name)
                );
            """))
            print("--- ✅ Táblák sikeresen létrehozva/ellenőrizve ---")

            # 2. ADATOK FELTÖLTÉSE
            print(f"--- 📥 Adatok betöltése... ---")
            for make, models in INITIAL_DATA.items():
                await conn.execute(
                    text("INSERT INTO ref.vehicle_makes (name) VALUES (:name) ON CONFLICT (name) DO NOTHING"),
                    {"name": make}
                )
                
                result = await conn.execute(
                    text("SELECT id FROM ref.vehicle_makes WHERE name = :name"),
                    {"name": make}
                )
                make_id = result.scalar()
                
                for model in models:
                    category = "Motor" if "(Motor)" in model else "Autó"
                    clean_model = model.replace(" (Motor)", "")
                    
                    await conn.execute(
                        text("""
                            INSERT INTO ref.vehicle_models (make_id, model_name, category) 
                            VALUES (:make_id, :name, :cat) 
                            ON CONFLICT DO NOTHING
                        """),
                        {"make_id": make_id, "name": clean_model, "cat": category}
                    )
                    print(f"   -> Mentve: {make} {clean_model}")

        print("\n--- 🏁 SIKER! Minden adat a helyén van. ---")
    
    except Exception as e:
        print(f"\n❌ KRITIKUS HIBA: {e}")
    
    finally:
        await engine.dispose()

if __name__ == "__main__":
    asyncio.run(seed_vehicles())
