#!/usr/bin/env python3
"""Einmaliges Migrationsskript: Generiert Haiku-Labels fuer alle bestehenden Lagen.

Ausfuehrung auf dem Monitor-Server:
  cd /home/claude-dev/AegisSight-Monitor
  .venvs_run: /home/claude-dev/.venvs/osint/bin/python migrate_category_labels.py
"""
import asyncio
import json
import logging
import os
import sys

# Projektpfad setzen damit imports funktionieren
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(name)s] %(levelname)s: %(message)s',
)
logger = logging.getLogger("migrate_labels")


async def main():
    from database import get_db
    from agents.geoparsing import generate_category_labels

    db = await get_db()
    try:
        # Alle Incidents ohne category_labels laden
        cursor = await db.execute(
            "SELECT id, title, description FROM incidents WHERE category_labels IS NULL"
        )
        incidents = [dict(row) for row in await cursor.fetchall()]

        if not incidents:
            logger.info("Keine Incidents ohne Labels gefunden. Nichts zu tun.")
            return

        logger.info(f"{len(incidents)} Incidents ohne Labels gefunden. Starte Generierung...")

        success = 0
        for inc in incidents:
            incident_id = inc["id"]
            context = f"{inc['title']} - {inc.get('description') or ''}"
            logger.info(f"Generiere Labels fuer Incident {incident_id}: {inc['title'][:60]}...")

            try:
                labels = await generate_category_labels(context)
                if labels:
                    await db.execute(
                        "UPDATE incidents SET category_labels = ? WHERE id = ?",
                        (json.dumps(labels, ensure_ascii=False), incident_id),
                    )
                    await db.commit()
                    success += 1
                    logger.info(f"  -> Labels: {labels}")
                else:
                    logger.warning(f"  -> Keine Labels generiert")
            except Exception as e:
                logger.error(f"  -> Fehler: {e}")

            # Kurze Pause um Rate-Limits zu vermeiden
            await asyncio.sleep(0.5)

        logger.info(f"\nMigration abgeschlossen: {success}/{len(incidents)} Incidents mit Labels versehen.")

    finally:
        await db.close()


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