import asyncio
from sqlalchemy import text
from app.db.session import engine

# 200+ Márka és Típus adatok
DATA = {
    "CAR": {
            "Toyota": ["Corolla", "Yaris", "RAV4", "Hilux", "C-HR", "Land Cruiser", "Camry"],
                    "Volkswagen": ["Golf", "Passat", "Polo", "Tiguan", "T-Roc", "Arteon", "Caddy"],
                            "BMW": ["3 Series", "5 Series", "X5", "X3", "1 Series", "7 Series", "X1"],
                                    "Mercedes-Benz": ["C-Class", "E-Class", "S-Class", "GLC", "GLE", "A-Class", "CLA"],
                                            "Audi": ["A3", "A4", "A6", "Q3", "Q5", "Q7", "A5", "TT"],
                                                    "Ford": ["Focus", "Fiesta", "Kuga", "Puma", "Transit", "Ranger", "Mondeo", "Mustang"],
                                                            "Opel": ["Astra", "Corsa", "Insignia", "Mokka", "Grandland", "Crossland", "Vivaro"],
                                                                    "Suzuki": ["Swift", "Vitara", "S-Cross", "Ignis", "Jimny", "Baleno"],
                                                                            "Skoda": ["Octavia", "Fabia", "Superb", "Kodiaq", "Karoq", "Kamiq", "Scala"],
                                                                                    "Hyundai": ["i30", "Tucson", "i20", "Kona", "Santa Fe", "Ioniq 5", "Bayon"],
                                                                                            "Kia": ["Ceed", "Sportage", "Rio", "Niro", "Sorento", "Picanto", "Stonic"],
                                                                                                    "Renault": ["Clio", "Megane", "Captur", "Kadjar", "Master", "Trafic", "Zoe", "Arkana"],
                                                                                                            "Peugeot": ["208", "308", "2008", "3008", "5008", "508", "Rifter"],
                                                                                                                    "Volvo": ["XC60", "XC40", "XC90", "V60", "S60", "V90", "S90"],
                                                                                                                            "Mazda": ["CX-5", "Mazda3", "Mazda6", "CX-30", "MX-5", "CX-3"],
                                                                                                                                    "Fiat": ["500", "Panda", "Tipo", "Ducato", "Doblo", "500X"],
                                                                                                                                            "Dacia": ["Duster", "Sandero", "Logan", "Jogger", "Spring"],
                                                                                                                                                    "Nissan": ["Qashqai", "Juke", "X-Trail", "Leaf", "Micra", "Navara"],
                                                                                                                                                            "Tesla": ["Model 3", "Model Y", "Model S", "Model X"],
                                                                                                                                                                    "Lexus": ["RX", "NX", "UX", "ES", "IS", "LS"]
                                                                                                                                                                        },
                                                                                                                                                                            "MOTORCYCLE": {
                                                                                                                                                                                    "Honda": ["CB500", "CBR600", "Africa Twin", "NC750X", "Goldwing", "PCX", "SH125", "Forza 350"],
                                                                                                                                                                                            "Yamaha": ["MT-07", "MT-09", "R1", "R6", "Tracer 9", "Ténéré 700", "XMAX", "TMAX"],
                                                                                                                                                                                                    "Kawasaki": ["Ninja 400", "Ninja 650", "Z900", "Z650", "Versys 650", "Vulcan S", "Z1000"],
                                                                                                                                                                                                            "Suzuki": ["V-Strom 650", "GSX-R1000", "Hayabusa", "SV650", "Burgman", "GSX-S1000"],
                                                                                                                                                                                                                    "BMW Motorrad": ["R1250GS", "S1000RR", "F850GS", "R nineT", "G310GS", "K1600GT"],
                                                                                                                                                                                                                            "KTM": ["Duke 390", "Duke 790", "1290 Super Adventure", "300 EXC", "890 Adventure"],
                                                                                                                                                                                                                                    "Ducati": ["Monster", "Multistrada", "Panigale V4", "Scrambler", "Diavel", "Streetfighter"],
                                                                                                                                                                                                                                            "Harley-Davidson": ["Sportster", "Fat Boy", "Iron 883", "Pan America", "Road Glide"]
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                async def run_discovery():
                                                                                                                                                                                                                                                    async with engine.begin() as conn:
                                                                                                                                                                                                                                                            print("🚀 Jármű adatbázis mély-feltöltése indul...")
                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                            for cat_name, brands in DATA.items():
                                                                                                                                                                                                                                                                                        res = await conn.execute(text("SELECT id FROM data.vehicle_categories WHERE name = :n"), {"n": cat_name})
                                                                                                                                                                                                                                                                                                    cat_id = res.scalar()
                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                            for brand_name, models in brands.items():
                                                                                                                                                                                                                                                                                                                                            # Márka beszúrása
                                                                                                                                                                                                                                                                                                                                                            await conn.execute(text(
                                                                                                                                                                                                                                                                                                                                                                                "INSERT INTO data.vehicle_brands (category_id, name) VALUES (:c, :n) ON CONFLICT (category_id, name) DO NOTHING"
                                                                                                                                                                                                                                                                                                                                                                                                ), {"c": cat_id, "n": brand_name})
                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                # Márka ID lekérése
                                                                                                                                                                                                                                                                                                                                                                                                                                                res_b = await conn.execute(text("SELECT id FROM data.vehicle_brands WHERE name = :n AND category_id = :c"), {"n": brand_name, "c": cat_id})
                                                                                                                                                                                                                                                                                                                                                                                                                                                                brand_id = res_b.scalar()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                for m_name in models:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    await conn.execute(text(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "INSERT INTO data.vehicle_models (brand_id, name) VALUES (:b, :n) ON CONFLICT (brand_id, name) DO NOTHING"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ), {"b": brand_id, "n": m_name})
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                print("✅ Discovery Bot sikeresen betöltötte az adatokat!")

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