import asyncio
import time
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text

async def main():
    url = "postgresql+asyncpg://admin:password@localhost:5432/echoesoftheashes"
    engine = create_async_engine(url)
    try:
        async with engine.begin() as conn:
            res = await conn.execute(text("SELECT id, location_id FROM characters WHERE name ILIKE 'Jocaru'"))
            row = res.first()
            if not row:
                print("Jocaru not found.")
                return
            pid, loc = row[0], row[1]
            print(f"Player Jocaru found (ID {pid}) at {loc}")
            
            await conn.execute(text("UPDATE characters SET level = 50, xp = 50000, max_hp = 500, hp = 500, max_stamina = 200, stamina = 200 WHERE id = :pid"), {"pid": pid})
            
            items = [
                ('reinforced_pack', 1),
                ('reinforced_bat', 1),
                ('combat_knife', 1),
                ('first_aid_kit', 10),
                ('mystery_pills', 5),
                ('energy_bar', 10)
            ]
            for iid, qty in items:
                await conn.execute(text("INSERT INTO inventory (character_id, item_id, quantity) VALUES (:pid, :iid, :qty)"), {"pid": pid, "iid": iid, "qty": qty})
            
            now = time.time()
            despawn = now + 86400
            enemies = ['raider_scout'] * 5 + ['feral_dog'] * 5 + ['mutant_rat'] * 5 + ['test_boss'] * 2
            for eid in enemies:
                await conn.execute(text("INSERT INTO wandering_enemies (npc_id, location_id, spawn_timestamp, despawn_timestamp) VALUES (:nid, :loc, :start, :end)"),
                    {"nid": eid, "loc": loc, "start": now, "end": despawn})
            print(f"Spawned {len(enemies)} enemies at {loc}.")
    except Exception as e:
        print(f"DB Error: {e}")

asyncio.run(main())
