# /opt/docker/dev/service_finder/backend/app/test_outside/robot_dashboard.py
import asyncio
import sys
from sqlalchemy import text
from app.database import AsyncSessionLocal

async def run_dashboard():
    print("\n" + "="*60)
    print("🤖 ROBOT HADOSZTÁLY ÁLLAPOTJELENTÉS 🤖")
    print("="*60)

    async with AsyncSessionLocal() as db:
        # --- 1. DISCOVERY (Felfedezés) ---
        print("\n📡 1. FÁZIS: Felfedezés (Discovery Engine)")
        print("-" * 40)
        res = await db.execute(text("SELECT status, count(*) FROM vehicle.catalog_discovery GROUP BY status ORDER BY count DESC"))
        rows = res.fetchall()
        if not rows: print("  Nincs adat.")
        for row in rows: print(f"  - {row[0].upper().ljust(20)}: {row[1]} db")

        # --- 2. FELDOLGOZÁS (Hunter, Researcher, Alchemist) ---
        print("\n⚙️ 2. FÁZIS: Feldolgozás és Tisztítás (Köztes tábla)")
        print("-" * 40)
        res = await db.execute(text("SELECT status, count(*) FROM vehicle.vehicle_model_definitions GROUP BY status ORDER BY count DESC"))
        rows = res.fetchall()
        if not rows: print("  Nincs adat.")
        for row in rows: print(f"  - {row[0].upper().ljust(20)}: {row[1]} db")

        # --- 3. HIBÁK (Kritikus elakadások) ---
        print("\n🚨 LEGGYAKORIBB HIBÁK (Top 3 felfüggesztett)")
        print("-" * 40)
        res = await db.execute(text("""
            SELECT substring(last_error from 1 for 70) as err, count(*)
            FROM vehicle.vehicle_model_definitions
            WHERE status = 'suspended' AND last_error IS NOT NULL
            GROUP BY err ORDER BY count DESC LIMIT 3
        """))
        errors = res.fetchall()
        if errors:
            for row in errors: print(f"  - [{row[1]} db] {row[0]}...")
        else:
            print("  - Nincs felfüggesztett, hibás rekord! 🎉")

        # --- 4. ARANY REKORDOK (Végleges) ---
        print("\n🏆 3. FÁZIS: Végleges Arany Katalógus")
        print("-" * 40)
        res = await db.execute(text("SELECT count(*) FROM vehicle.vehicle_catalog"))
        print(f"  - Kész járművek száma : {res.scalar()} db")
        
    print("\n" + "="*60 + "\n")

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

# docker exec -it sf_api python /app/app/test_outside/robot_dashboard.py