#!/usr/bin/env python3
"""
Diagnostic complet du système
"""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent / "geniusia2"))

from core.config import get_config
from core.logger import Logger
from core.embeddings_manager import EmbeddingsManager
from core.learning_manager import LearningManager

def main():
    print("\n" + "="*60)
    print("  🔍 DIAGNOSTIC COMPLET")
    print("="*60 + "\n")
    
    config = get_config()
    logger = Logger()
    
    # 1. Vérifier les tâches
    print("📊 Tâches apprises:")
    embeddings_manager = EmbeddingsManager(logger=logger)
    learning_manager = LearningManager(
        embeddings_manager,
        logger,
        config,
        profiles_path="geniusia2/data/user_profiles"
    )
    
    tasks = learning_manager.get_all_tasks()
    print(f"   Total: {len(tasks)} tâches\n")
    
    if tasks:
        print("   Dernières tâches:")
        for task in tasks[-5:]:
            print(f"     - {task['task_name']} (confiance: {task['confidence_score']:.2f})")
    
    # 2. Vérifier l'index FAISS
    print(f"\n🧠 Index FAISS:")
    index_size = embeddings_manager.get_index_size()
    print(f"   Embeddings: {index_size}")
    
    # 3. Vérifier la config
    print(f"\n⚙️  Configuration:")
    print(f"   enforce_whitelist: {config['security']['enforce_whitelist']}")
    print(f"   similarity_threshold: {config.get('assist', {}).get('similarity_threshold', 0.75)}")
    print(f"   suggestion_timeout: {config.get('assist', {}).get('suggestion_timeout', 10.0)}")
    
    # 4. Diagnostic
    print(f"\n" + "="*60)
    print("  💡 DIAGNOSTIC")
    print("="*60 + "\n")
    
    if len(tasks) == 0:
        print("❌ Aucune tâche apprise")
        print("   Solution: Fais 3x la même action")
    elif index_size == 0:
        print("❌ Index FAISS vide")
        print("   Solution: Reconstruis l'index")
        print("   $ geniusia2/venv/bin/python rebuild_faiss_simple.py")
    else:
        print("✅ Tâches et index OK")
        print()
        print("Le Mode Assisté devrait fonctionner !")
        print()
        print("Pour tester:")
        print("  1. Lance l'app: cd geniusia2 && ./run.sh")
        print("  2. Clique sur 'Start'")
        print("  3. Fais 3x la MÊME action (ex: 3 clics au même endroit)")
        print("  4. Attends 'Tâche apprise !'")
        print("  5. Refais 1x la MÊME action")
        print("  6. L'overlay devrait apparaître")
        print()
        print("⚠️  IMPORTANT:")
        print("  - Il faut faire la MÊME action 3 fois")
        print("  - Pas un workflow complexe")
        print("  - Juste: clic, clic, clic au même endroit")


if __name__ == "__main__":
    main()
