#!/usr/bin/env python3
"""
Checkpoint - Validation du Mode Complet
Vérifie que tous les composants de la Phase 3 fonctionnent correctement.
"""

import sys
import numpy as np
from pathlib import Path

# Ajouter le répertoire parent au path
sys.path.insert(0, str(Path(__file__).parent))

print("\n" + "="*70)
print("CHECKPOINT - MODE COMPLET")
print("Validation de la Phase 3 - Fusion Multi-Modale")
print("="*70)

# ============================================================================
# 1. Vérification des Imports
# ============================================================================
print("\n📦 1. Vérification des imports...")

try:
    from geniusia2.core.enriched_screen_capture import EnrichedScreenCapture
    print("   ✅ EnrichedScreenCapture")
except ImportError as e:
    print(f"   ❌ EnrichedScreenCapture: {e}")
    sys.exit(1)

try:
    from geniusia2.core.multimodal_embedding_manager import (
        MultiModalEmbeddingManager,
        EmbeddingWeights
    )
    print("   ✅ MultiModalEmbeddingManager")
    print("   ✅ EmbeddingWeights")
except ImportError as e:
    print(f"   ❌ MultiModalEmbeddingManager: {e}")
    sys.exit(1)

try:
    from geniusia2.core.enhanced_workflow_matcher import (
        EnhancedWorkflowMatcher,
        WorkflowMatch,
        ElementMatch
    )
    print("   ✅ EnhancedWorkflowMatcher")
    print("   ✅ WorkflowMatch")
    print("   ✅ ElementMatch")
except ImportError as e:
    print(f"   ❌ EnhancedWorkflowMatcher: {e}")
    sys.exit(1)

try:
    from geniusia2.core.ui_element_models import (
        EnrichedScreenState,
        UIElement,
        StateEmbedding
    )
    print("   ✅ EnrichedScreenState")
    print("   ✅ UIElement")
    print("   ✅ StateEmbedding")
except ImportError as e:
    print(f"   ❌ ui_element_models: {e}")
    sys.exit(1)

try:
    from geniusia2.core.logger import Logger
    print("   ✅ Logger")
except ImportError as e:
    print(f"   ❌ Logger: {e}")
    sys.exit(1)

print("\n✅ Tous les imports sont OK")

# ============================================================================
# 2. Test des 3 Modes
# ============================================================================
print("\n🔧 2. Test des 3 modes...")

logger = Logger(log_dir="checkpoint_logs")

# Mode Light
print("\n   Mode Light:")
try:
    capture_light = EnrichedScreenCapture(
        logger=logger,
        data_dir="checkpoint_data",
        mode="light"
    )
    assert capture_light.get_mode() == "light"
    assert capture_light.ui_detector is None
    assert capture_light.multimodal_manager is None
    assert capture_light.enhanced_matcher is None
    print("      ✅ Mode Light OK")
except Exception as e:
    print(f"      ❌ Mode Light: {e}")
    sys.exit(1)

# Mode Enriched
print("\n   Mode Enriched:")
try:
    capture_enriched = EnrichedScreenCapture(
        logger=logger,
        data_dir="checkpoint_data",
        mode="enriched"
    )
    assert capture_enriched.get_mode() == "enriched"
    assert capture_enriched.ui_detector is not None
    assert capture_enriched.multimodal_manager is None
    assert capture_enriched.enhanced_matcher is None
    print("      ✅ Mode Enriched OK")
except Exception as e:
    print(f"      ❌ Mode Enriched: {e}")
    sys.exit(1)

# Mode Complete
print("\n   Mode Complete:")
try:
    capture_complete = EnrichedScreenCapture(
        logger=logger,
        data_dir="checkpoint_data",
        mode="complete"
    )
    assert capture_complete.get_mode() == "complete"
    assert capture_complete.ui_detector is not None
    assert capture_complete.multimodal_manager is not None
    assert capture_complete.enhanced_matcher is not None
    print("      ✅ Mode Complete OK")
except Exception as e:
    print(f"      ❌ Mode Complete: {e}")
    sys.exit(1)

print("\n✅ Les 3 modes fonctionnent correctement")

# ============================================================================
# 3. Test du Changement de Mode Dynamique
# ============================================================================
print("\n🔄 3. Test du changement de mode dynamique...")

try:
    capture = EnrichedScreenCapture(
        logger=logger,
        data_dir="checkpoint_data",
        mode="light"
    )
    
    # Light → Enriched
    capture.set_mode("enriched")
    assert capture.get_mode() == "enriched"
    assert capture.ui_detector is not None
    print("   ✅ Light → Enriched")
    
    # Enriched → Complete
    capture.set_mode("complete")
    assert capture.get_mode() == "complete"
    assert capture.multimodal_manager is not None
    assert capture.enhanced_matcher is not None
    print("   ✅ Enriched → Complete")
    
    # Complete → Light
    capture.set_mode("light")
    assert capture.get_mode() == "light"
    assert capture.ui_detector is None
    assert capture.multimodal_manager is None
    assert capture.enhanced_matcher is None
    print("   ✅ Complete → Light")
    
    print("\n✅ Changement de mode dynamique OK")
except Exception as e:
    print(f"\n❌ Changement de mode: {e}")
    sys.exit(1)

# ============================================================================
# 4. Test de MultiModalEmbeddingManager
# ============================================================================
print("\n🧠 4. Test de MultiModalEmbeddingManager...")

try:
    manager = MultiModalEmbeddingManager(
        logger=logger,
        data_dir="checkpoint_data",
        config={
            "embedding_dim": 256,
            "weights": {
                "image": 0.4,
                "text": 0.3,
                "title": 0.1,
                "ui": 0.1,
                "context": 0.1
            }
        }
    )
    
    # Test poids
    assert manager.embedding_dim == 256
    assert abs(sum(manager.default_weights.to_dict().values()) - 1.0) < 0.001
    print("   ✅ Configuration des poids OK")
    
    # Test similarité
    emb1 = np.random.rand(256)
    emb2 = np.random.rand(256)
    emb3 = emb1.copy()
    
    sim_random = manager.compute_similarity(emb1, emb2)
    sim_identical = manager.compute_similarity(emb1, emb3)
    
    assert 0.0 <= sim_random <= 1.0
    assert 0.95 <= sim_identical <= 1.05
    print("   ✅ Calcul de similarité OK")
    
    print("\n✅ MultiModalEmbeddingManager OK")
except Exception as e:
    print(f"\n❌ MultiModalEmbeddingManager: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# ============================================================================
# 5. Test de EnhancedWorkflowMatcher
# ============================================================================
print("\n🎯 5. Test de EnhancedWorkflowMatcher...")

try:
    multimodal_manager = MultiModalEmbeddingManager(
        logger=logger,
        data_dir="checkpoint_data"
    )
    
    matcher = EnhancedWorkflowMatcher(
        multimodal_manager=multimodal_manager,
        logger=logger,
        config={
            "screen_weight": 0.6,
            "elements_weight": 0.4
        }
    )
    
    assert matcher.screen_weight == 0.6
    assert matcher.elements_weight == 0.4
    print("   ✅ Configuration du matcher OK")
    
    # Test matching (sans workflows)
    from geniusia2.core.screen_state_manager import ScreenStateManager
    
    screen_manager = ScreenStateManager(
        logger=logger,
        data_dir="checkpoint_data"
    )
    
    screen_state = screen_manager.create_screen_state(
        session_id="checkpoint_session",
        window_title="Test Window",
        app_name="TestApp",
        screenshot_path="test.png",
        screen_resolution=(1920, 1080)
    )
    
    matches = matcher.find_matching_workflows(
        screen_state=screen_state,
        screenshot=None,
        workflows=[],
        top_k=5
    )
    
    assert isinstance(matches, list)
    print("   ✅ Matching de workflows OK")
    
    print("\n✅ EnhancedWorkflowMatcher OK")
except Exception as e:
    print(f"\n❌ EnhancedWorkflowMatcher: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# ============================================================================
# 6. Test d'Intégration Complète
# ============================================================================
print("\n🔗 6. Test d'intégration complète...")

try:
    # Créer le système complet
    capture = EnrichedScreenCapture(
        logger=logger,
        data_dir="checkpoint_data",
        mode="complete"
    )
    
    # Créer un screenshot simulé
    screenshot = np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8)
    
    # Capturer et enrichir
    screen_state = capture.capture_and_enrich(
        screenshot=screenshot,
        session_id="checkpoint_session",
        window_title="Test Application",
        app_name="TestApp",
        screen_resolution=(1920, 1080),
        detected_text=["Button", "Submit", "Cancel"],
        context_tags=["form", "validation"],
        workflow_candidate="test_workflow",
        save=False
    )
    
    assert screen_state is not None
    assert screen_state.mode == "complete"
    assert screen_state.state_embedding is not None
    print("   ✅ Capture et enrichissement OK")
    
    # Test matching
    matches = capture.find_matching_workflows(
        screen_state=screen_state,
        screenshot=screenshot,
        workflows=[],
        top_k=5
    )
    
    assert isinstance(matches, list)
    print("   ✅ Matching intégré OK")
    
    print("\n✅ Intégration complète OK")
except Exception as e:
    print(f"\n❌ Intégration: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# ============================================================================
# 7. Vérification des Tests Existants
# ============================================================================
print("\n🧪 7. Vérification des tests existants...")

test_files = [
    "test_ui_element_phase1.py",
    "test_ui_element_phase2.py",
    "test_ui_element_phase3.py"
]

for test_file in test_files:
    if Path(test_file).exists():
        print(f"   ✅ {test_file} existe")
    else:
        print(f"   ⚠️  {test_file} manquant")

print("\n✅ Tests existants vérifiés")

# ============================================================================
# 8. Nettoyage
# ============================================================================
print("\n🧹 8. Nettoyage...")

import shutil

if Path("checkpoint_data").exists():
    shutil.rmtree("checkpoint_data")
    print("   ✅ checkpoint_data supprimé")

if Path("checkpoint_logs").exists():
    shutil.rmtree("checkpoint_logs")
    print("   ✅ checkpoint_logs supprimé")

# ============================================================================
# RÉSUMÉ FINAL
# ============================================================================
print("\n" + "="*70)
print("✅ CHECKPOINT RÉUSSI - MODE COMPLET VALIDÉ")
print("="*70)

print("\n📊 Résumé:")
print("   ✅ Imports: OK")
print("   ✅ 3 Modes: OK")
print("   ✅ Changement dynamique: OK")
print("   ✅ MultiModalEmbeddingManager: OK")
print("   ✅ EnhancedWorkflowMatcher: OK")
print("   ✅ Intégration complète: OK")
print("   ✅ Tests existants: OK")

print("\n🎉 Le Mode Complet est OPÉRATIONNEL!")
print("\n📝 Prochaines étapes recommandées:")
print("   1. Tester avec des workflows réels")
print("   2. Améliorer le matching (Phase 4)")
print("   3. Optimiser les performances (Phase 5)")
print("   4. Créer des outils utilisateur (Phase 6)")

print("\n" + "="*70)
sys.exit(0)
