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

load_dotenv()

raw_url = os.getenv("DATABASE_URL")
if not raw_url:
    raw_url = "postgresql://admin:PASSWORD_111@postgres-db:5432/service_finder"
DATABASE_URL = raw_url.replace("postgresql://", "postgresql+asyncpg://").replace("/service_finder_db", "/service_finder")

async def hire_driver():
    engine = create_async_engine(DATABASE_URL)

    async with engine.begin() as conn:
        print("👤 Kovács János (User ID: 2) létrehozása...")
        # Létrehozzuk a User-t
        await conn.execute(text("""
            INSERT INTO data.users (id, email, password_hash, role, country, default_currency, is_active)
            VALUES (2, 'sofor@ceg.hu', 'hash123', 'PRIVATE', 'HU', 'HUF', TRUE)
            ON CONFLICT (id) DO NOTHING;
        """))
        
        # Frissítjük a sorrendet
        await conn.execute(text("SELECT setval('data.users_id_seq', (SELECT MAX(id) FROM data.users));"))

        print("🤝 Hozzárendelés a Te cégedhez (ID: 1)...")
        # Betesszük a fleet_members táblába
        await conn.execute(text("""
            INSERT INTO data.fleet_members (user_id, owner_id, role)
            VALUES (2, 1, 'DRIVER')
            ON CONFLICT (user_id, owner_id) DO NOTHING;
        """))

    print("✅ KÉSZ! Kovács János mostantól a csapatod tagja.")
    await engine.dispose()

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