#!/usr/bin/env python3
"""
Test des améliorations du SuggestionManager pour le Mode Assisté.
"""

import sys
import os

# Ajouter le chemin du projet
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'geniusia2'))

from geniusia2.core.suggestion_manager import SuggestionManager
from geniusia2.core.workflow_matcher import WorkflowMatch
from geniusia2.core.learning_manager import LearningManager
from geniusia2.core.embeddings_manager import EmbeddingsManager
from geniusia2.core.logger import Logger


class MockLearningManager:
    """Mock du LearningManager pour les tests."""
    def confirm_action(self, data):
        pass


class MockEmbeddingsManager:
    """Mock de l'EmbeddingsManager pour les tests."""
    def search_similar(self, embedding, k=3):
        return []


def test_check_workflow_match():
    """Test de la méthode check_workflow_match."""
    print("\n1. Test check_workflow_match")
    print("=" * 60)
    
    # Configuration
    config = {
        "assist": {
            "similarity_threshold": 0.75,
            "suggestion_timeout": 10.0
        },
        "workflow": {
            "position_tolerance": 50,
            "min_confidence": 0.80
        }
    }
    
    # Créer les dépendances
    logger = Logger(config)
    learning_manager = MockLearningManager()
    embeddings_manager = MockEmbeddingsManager()
    
    # Créer le SuggestionManager
    suggestion_manager = SuggestionManager(
        learning_manager,
        embeddings_manager,
        logger,
        config
    )
    
    # Créer des actions de session
    session_actions = [
        {
            "action_type": "click",
            "position": [100, 100],
            "window": "Calculatrice"
        },
        {
            "action_type": "type",
            "position": [0, 0],
            "window": "Calculatrice"
        }
    ]
    
    # Créer un workflow
    workflow = {
        "workflow_id": "calc_001",
        "name": "Calcul simple",
        "steps": [
            {
                "action_type": "click",
                "position": [100, 100],
                "window": "Calculatrice"
            },
            {
                "action_type": "type",
                "position": [0, 0],
                "window": "Calculatrice"
            },
            {
                "action_type": "click",
                "position": [200, 200],
                "window": "Calculatrice"
            }
        ]
    }
    
    # Vérifier le match
    match = suggestion_manager.check_workflow_match(session_actions, [workflow])
    
    if match:
        print(f"✓ Match trouvé: {match.workflow_name}")
        print(f"  Confiance: {match.confidence:.2%}")
        print(f"  Étapes matchées: {match.matched_steps}/{match.total_steps}")
        print(f"  Étapes restantes: {len(match.remaining_steps)}")
    else:
        print("✗ Aucun match trouvé")
        return False
    
    return True


def test_create_workflow_suggestion():
    """Test de la méthode create_workflow_suggestion."""
    print("\n2. Test create_workflow_suggestion")
    print("=" * 60)
    
    # Configuration
    config = {
        "assist": {
            "similarity_threshold": 0.75,
            "suggestion_timeout": 10.0
        },
        "workflow": {
            "position_tolerance": 50,
            "min_confidence": 0.80
        }
    }
    
    # Créer les dépendances
    logger = Logger(config)
    learning_manager = MockLearningManager()
    embeddings_manager = MockEmbeddingsManager()
    
    # Créer le SuggestionManager
    suggestion_manager = SuggestionManager(
        learning_manager,
        embeddings_manager,
        logger,
        config
    )
    
    # Créer un WorkflowMatch
    workflow_match = WorkflowMatch(
        workflow_id="calc_001",
        workflow_name="Calcul simple",
        confidence=0.85,
        matched_steps=2,
        total_steps=5,
        remaining_steps=[
            {"action_type": "click", "position": [200, 200]},
            {"action_type": "type", "text": "123"},
            {"action_type": "click", "position": [300, 300]}
        ],
        current_step_index=2
    )
    
    # Créer la suggestion
    suggestion = suggestion_manager.create_workflow_suggestion(workflow_match)
    
    if suggestion:
        print(f"✓ Suggestion créée: {suggestion['workflow_name']}")
        print(f"  Type: {suggestion['type']}")
        print(f"  Confiance: {suggestion['confidence']:.2%}")
        print(f"  Étape courante: {suggestion['current_step']}/{suggestion['total_steps']}")
        print(f"  Étapes restantes: {len(suggestion['remaining_steps'])}")
        print(f"  Aperçu (3 prochaines): {len(suggestion['next_steps_preview'])}")
    else:
        print("✗ Échec de création de suggestion")
        return False
    
    return True


def test_rejection_tracking():
    """Test du tracking des rejets et ajustement de priorité."""
    print("\n3. Test rejection tracking")
    print("=" * 60)
    
    # Configuration
    config = {
        "assist": {
            "similarity_threshold": 0.75,
            "suggestion_timeout": 10.0
        },
        "workflow": {
            "position_tolerance": 50,
            "min_confidence": 0.80
        }
    }
    
    # Créer les dépendances
    logger = Logger(config)
    learning_manager = MockLearningManager()
    embeddings_manager = MockEmbeddingsManager()
    
    # Créer le SuggestionManager
    suggestion_manager = SuggestionManager(
        learning_manager,
        embeddings_manager,
        logger,
        config
    )
    
    workflow_id = "calc_001"
    
    # Simuler 5 rejets
    print(f"  Simulation de 5 rejets pour workflow {workflow_id}:")
    for i in range(5):
        suggestion_manager._track_workflow_rejection(workflow_id)
        rejections = suggestion_manager.workflow_rejections.get(workflow_id, 0)
        adjustment = suggestion_manager.workflow_priority_adjustments.get(workflow_id, 1.0)
        print(f"    Rejet {i+1}: rejections={rejections}, adjustment={adjustment:.3f}")
    
    # Vérifier l'ajustement
    final_adjustment = suggestion_manager.workflow_priority_adjustments.get(workflow_id, 1.0)
    if final_adjustment < 1.0:
        print(f"✓ Priorité ajustée après 3+ rejets: {final_adjustment:.3f}")
    else:
        print("✗ Priorité non ajustée")
        return False
    
    # Tester une acceptation
    print(f"\n  Simulation d'une acceptation:")
    suggestion_manager._track_workflow_acceptance(workflow_id)
    rejections_after = suggestion_manager.workflow_rejections.get(workflow_id, 0)
    adjustment_after = suggestion_manager.workflow_priority_adjustments.get(workflow_id, 1.0)
    print(f"    Après acceptation: rejections={rejections_after}, adjustment={adjustment_after:.3f}")
    
    if rejections_after < 5:
        print(f"✓ Compteur de rejets réduit après acceptation")
    else:
        print("✗ Compteur de rejets non réduit")
        return False
    
    return True


def test_priority_adjustment_application():
    """Test de l'application de l'ajustement de priorité."""
    print("\n4. Test priority adjustment application")
    print("=" * 60)
    
    # Configuration
    config = {
        "assist": {
            "similarity_threshold": 0.75,
            "suggestion_timeout": 10.0
        },
        "workflow": {
            "position_tolerance": 50,
            "min_confidence": 0.80
        }
    }
    
    # Créer les dépendances
    logger = Logger(config)
    learning_manager = MockLearningManager()
    embeddings_manager = MockEmbeddingsManager()
    
    # Créer le SuggestionManager
    suggestion_manager = SuggestionManager(
        learning_manager,
        embeddings_manager,
        logger,
        config
    )
    
    workflow_id = "calc_001"
    
    # Créer un match avec confiance élevée
    match = WorkflowMatch(
        workflow_id=workflow_id,
        workflow_name="Calcul simple",
        confidence=0.90,
        matched_steps=2,
        total_steps=5,
        remaining_steps=[],
        current_step_index=2
    )
    
    print(f"  Confiance originale: {match.confidence:.2%}")
    
    # Appliquer sans ajustement
    adjusted_1 = suggestion_manager._apply_priority_adjustment(match)
    print(f"  Confiance sans ajustement: {adjusted_1:.2%}")
    
    # Ajouter des rejets pour créer un ajustement
    for _ in range(3):
        suggestion_manager._track_workflow_rejection(workflow_id)
    
    # Appliquer avec ajustement
    adjusted_2 = suggestion_manager._apply_priority_adjustment(match)
    print(f"  Confiance avec ajustement (3 rejets): {adjusted_2:.2%}")
    
    if adjusted_2 < adjusted_1:
        print(f"✓ Ajustement appliqué correctement")
    else:
        print("✗ Ajustement non appliqué")
        return False
    
    return True


def test_full_workflow():
    """Test du workflow complet: match -> suggestion -> reject/accept."""
    print("\n5. Test workflow complet")
    print("=" * 60)
    
    # Configuration
    config = {
        "assist": {
            "similarity_threshold": 0.75,
            "suggestion_timeout": 10.0
        },
        "workflow": {
            "position_tolerance": 50,
            "min_confidence": 0.80
        }
    }
    
    # Créer les dépendances
    logger = Logger(config)
    learning_manager = MockLearningManager()
    embeddings_manager = MockEmbeddingsManager()
    
    # Créer le SuggestionManager
    suggestion_manager = SuggestionManager(
        learning_manager,
        embeddings_manager,
        logger,
        config
    )
    
    # Créer des actions de session
    session_actions = [
        {
            "action_type": "click",
            "position": [100, 100],
            "window": "Calculatrice"
        },
        {
            "action_type": "type",
            "position": [0, 0],
            "window": "Calculatrice"
        }
    ]
    
    # Créer un workflow
    workflow = {
        "workflow_id": "calc_001",
        "name": "Calcul simple",
        "steps": [
            {
                "action_type": "click",
                "position": [100, 100],
                "window": "Calculatrice"
            },
            {
                "action_type": "type",
                "position": [0, 0],
                "window": "Calculatrice"
            },
            {
                "action_type": "click",
                "position": [200, 200],
                "window": "Calculatrice"
            }
        ]
    }
    
    # 1. Vérifier le match
    print("  Étape 1: Vérifier le match")
    match = suggestion_manager.check_workflow_match(session_actions, [workflow])
    if not match:
        print("✗ Aucun match trouvé")
        return False
    print(f"    ✓ Match trouvé: {match.workflow_name}")
    
    # 2. Créer la suggestion
    print("  Étape 2: Créer la suggestion")
    suggestion = suggestion_manager.create_workflow_suggestion(match)
    if not suggestion:
        print("✗ Échec de création de suggestion")
        return False
    print(f"    ✓ Suggestion créée")
    
    # 3. Vérifier qu'on a une suggestion active
    current = suggestion_manager.get_current_suggestion()
    if not current:
        print("✗ Pas de suggestion active")
        return False
    print(f"    ✓ Suggestion active")
    
    # 4. Rejeter la suggestion
    print("  Étape 3: Rejeter la suggestion")
    rejected = suggestion_manager.reject_suggestion()
    if not rejected:
        print("✗ Échec du rejet")
        return False
    print(f"    ✓ Suggestion rejetée")
    
    # 5. Vérifier le tracking du rejet
    rejections = suggestion_manager.workflow_rejections.get("calc_001", 0)
    print(f"    Rejets trackés: {rejections}")
    if rejections != 1:
        print("✗ Rejet non tracké")
        return False
    print(f"    ✓ Rejet tracké")
    
    print("\n✓ Workflow complet réussi!")
    return True


def main():
    """Fonction principale."""
    print("\n" + "=" * 60)
    print("Test des améliorations du SuggestionManager")
    print("=" * 60)
    
    tests = [
        test_check_workflow_match,
        test_create_workflow_suggestion,
        test_rejection_tracking,
        test_priority_adjustment_application,
        test_full_workflow
    ]
    
    results = []
    for test in tests:
        try:
            result = test()
            results.append(result)
        except Exception as e:
            print(f"\n✗ Erreur dans {test.__name__}: {e}")
            import traceback
            traceback.print_exc()
            results.append(False)
    
    # Résumé
    print("\n" + "=" * 60)
    print("RÉSUMÉ")
    print("=" * 60)
    passed = sum(results)
    total = len(results)
    print(f"Tests réussis: {passed}/{total}")
    
    if passed == total:
        print("\n✓ Tous les tests sont passés!")
        return 0
    else:
        print(f"\n✗ {total - passed} test(s) échoué(s)")
        return 1


if __name__ == "__main__":
    sys.exit(main())
