#!/usr/bin/env python3
"""
Debug en temps réel de la détection de patterns.
"""

import sys
import os
import time
import pickle

sys.path.insert(0, 'geniusia2')

print("="*60)
print("DEBUG DÉTECTION DE PATTERNS")
print("="*60)
print("\nCe script surveille:")
print("  - Les événements capturés")
print("  - Les sessions créées")
print("  - Les workflows détectés")
print("  - Les patterns trouvés")
print("\nAppuyez sur Ctrl+C pour arrêter\n")
print("="*60)

last_checkpoint_time = 0
last_index_time = 0

try:
    while True:
        time.sleep(2)
        
        # 1. Vérifier le checkpoint
        checkpoint_path = "data/fine_tuning/orchestrator_finetuning.pkl"
        if os.path.exists(checkpoint_path):
            mtime = os.path.getmtime(checkpoint_path)
            if mtime != last_checkpoint_time:
                last_checkpoint_time = mtime
                try:
                    with open(checkpoint_path, 'rb') as f:
                        data = pickle.load(f)
                    
                    total = len(data.get('positive_examples', [])) + len(data.get('negative_examples', []))
                    print(f"\n📊 Checkpoint mis à jour: {total} exemples")
                except:
                    pass
        
        # 2. Vérifier l'index FAISS
        index_path = "data/workflow_embeddings.index"
        if os.path.exists(index_path):
            mtime = os.path.getmtime(index_path)
            if mtime != last_index_time:
                last_index_time = mtime
                
                try:
                    with open("data/workflow_embeddings.metadata", 'rb') as f:
                        metadata = pickle.load(f)
                    
                    workflows = {}
                    for m in metadata.get('metadata', []):
                        wid = m.get('workflow_id', 'unknown')
                        workflows[wid] = workflows.get(wid, 0) + 1
                    
                    print(f"\n🔍 Index FAISS mis à jour:")
                    print(f"   - {len(metadata.get('metadata', []))} embeddings")
                    print(f"   - {len(workflows)} workflows")
                    for wid in list(workflows.keys())[-3:]:
                        print(f"     • {wid}: {workflows[wid]} embeddings")
                except:
                    pass

except KeyboardInterrupt:
    print("\n\n✋ Arrêt du monitoring")
    print("\nRésumé:")
    
    # Afficher l'état final
    if os.path.exists(checkpoint_path):
        try:
            with open(checkpoint_path, 'rb') as f:
                data = pickle.load(f)
            total = len(data.get('positive_examples', [])) + len(data.get('negative_examples', []))
            print(f"  - Exemples collectés: {total}")
        except:
            pass
    
    if os.path.exists(index_path):
        try:
            with open("data/workflow_embeddings.metadata", 'rb') as f:
                metadata = pickle.load(f)
            print(f"  - Embeddings indexés: {len(metadata.get('metadata', []))}")
        except:
            pass
    
    print("\n💡 Si rien ne bouge:")
    print("  1. Vérifie que l'app tourne: ps aux | grep main.py")
    print("  2. Regarde la sortie du terminal où tu as lancé ./run.sh")
    print("  3. Les messages '🎯 PATTERN DÉTECTÉ' s'affichent là-bas")
    print("")
