# /opt/docker/dev/service_finder/backend/app/scripts/morning_report.py
import asyncio
from sqlalchemy import select
from app.db.session import SessionLocal
from app.models import ProcessLog
from datetime import datetime, timedelta, timezone

async def generate_morning_report():
    """ Összesíti a háttérfolyamatok (robotok) elmúlt 24 órás teljesítményét. """
    async with SessionLocal() as db:
        yesterday = datetime.now(timezone.utc) - timedelta(days=1)
        stmt = select(ProcessLog).where(ProcessLog.start_time >= yesterday)
        res = await db.execute(stmt)
        logs = res.scalars().all()

        report = f"📊 REGGELI ROBOT JELENTÉS - {datetime.now().date()}\n"
        report += "="*40 + "\n"

        total_proc = sum(log.items_processed for log in logs)
        total_fail = sum(log.items_failed for log in logs)
        
        report += f"✅ Feldolgozott egységek: {total_proc}\n"
        report += f"❌ Sikertelen műveletek: {total_fail}\n"
        
        if logs:
            report += "\nAktív robotok állapota:\n"
            for log in logs:
                status = "🟢 OK" if log.items_failed == 0 else "🔴 HIBA"
                report += f"  - {log.process_name}: {log.items_processed} feldolgozva ({status})\n"

        print(report)
        return report

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