#!/usr/bin/env python3
# docker exec -it sf_api python -m app.scripts.monitor_crawler
import asyncio
import os
from sqlalchemy import text
from app.database import AsyncSessionLocal
from datetime import datetime

async def monitor():
    print(f"\n🛰️  AUTO-DATA CRAWLER MONITOR | {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print("=" * 60)
    
    async with AsyncSessionLocal() as db:
        # 1. Összesített statisztika szintenként
        stats_query = text("""
            SELECT level, status, COUNT(*) 
            FROM vehicle.auto_data_crawler_queue 
            GROUP BY level, status 
            ORDER BY level, status;
        """)
        
        # 2. Utolsó 5 hiba
        error_query = text("""
            SELECT name, level, error_msg, updated_at 
            FROM vehicle.auto_data_crawler_queue 
            WHERE status = 'error' 
            ORDER BY updated_at DESC LIMIT 5;
        """)

        res = await db.execute(stats_query)
        rows = res.fetchall()
        
        if not rows:
            print("📭 A várólista üres.")
        else:
            print(f"{'SZINT':<15} | {'STÁTUSZ':<12} | {'DARABSZÁM':<10}")
            print("-" * 45)
            for r in rows:
                icon = "⏳" if r[1] == 'pending' else "⚙️" if r[1] == 'processing' else "✅" if r[1] == 'completed' else "❌"
                print(f"{r[0].upper():<15} | {icon} {r[1]:<10} | {r[2]:<10}")

        errors = await db.execute(error_query)
        error_rows = errors.fetchall()
        
        if error_rows:
            print("\n🚨 LEGUTÓBBI HIBÁK:")
            print("-" * 60)
            for e in error_rows:
                print(f"📍 {e[0]} ({e[1]}): {e[2][:70]}... [{e[3].strftime('%H:%M:%S')}]")

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