"""
Test simple du LogsPanel sans interface graphique
"""

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

from PyQt5.QtWidgets import QApplication
from gui.logs_panel import LogsPanel, LogMessage
from datetime import datetime

# Créer une application Qt (nécessaire pour les widgets Qt)
app = QApplication(sys.argv)

def test_logs_panel():
    """Test des fonctionnalités du LogsPanel"""
    
    # Test 1: Création du panneau
    print('Test 1: Création du panneau')
    panel = LogsPanel()
    print(f'✓ Panneau créé, max_logs={panel.max_logs}')
    assert panel.max_logs == 30, f'Expected max_logs=30, got {panel.max_logs}'
    
    # Test 2: Ajout de logs
    print('\nTest 2: Ajout de logs')
    panel.add_log('Test message 1', '👀', 'info')
    panel.add_log('Test message 2', '🎯', 'success')
    panel.add_log('Test message 3', '⚠️', 'warning')
    print(f'✓ 3 logs ajoutés, count={panel.get_log_count()}')
    assert panel.get_log_count() == 3, f'Expected 3, got {panel.get_log_count()}'
    
    # Test 3: Vérifier la limite de 30
    print('\nTest 3: Test de la limite de 30 messages')
    for i in range(30):
        panel.add_log(f'Message {i+4}', '📝', 'info')
    print(f'✓ Ajouté 30 logs supplémentaires, count={panel.get_log_count()}')
    assert panel.get_log_count() == 30, f'Expected 30, got {panel.get_log_count()}'
    
    # Test 4: Vérifier que les anciens sont supprimés
    print('\nTest 4: Vérifier suppression des anciens')
    panel.add_log('Nouveau message', '✨', 'success')
    print(f'✓ Ajouté 1 log de plus, count={panel.get_log_count()}')
    assert panel.get_log_count() == 30, f'Expected 30, got {panel.get_log_count()}'
    
    # Test 5: get_last_log
    print('\nTest 5: get_last_log')
    last = panel.get_last_log()
    print(f'✓ Dernier log: {last.message}')
    assert last.message == 'Nouveau message', f'Expected "Nouveau message", got "{last.message}"'
    
    # Test 6: get_logs
    print('\nTest 6: get_logs')
    logs = panel.get_logs()
    print(f'✓ get_logs retourne {len(logs)} logs')
    assert len(logs) == 30, f'Expected 30, got {len(logs)}'
    
    # Test 7: Vérifier que le premier log a été supprimé
    print('\nTest 7: Vérifier que les premiers logs ont été supprimés')
    first_log = logs[0]
    print(f'✓ Premier log restant: {first_log.message}')
    # Les 3 premiers logs + 1 ont été supprimés, donc le premier devrait être "Message 4"
    assert 'Message' in first_log.message, f'Expected message with "Message", got "{first_log.message}"'
    
    # Test 8: clear
    print('\nTest 8: clear')
    panel.clear()
    print(f'✓ Logs effacés, count={panel.get_log_count()}')
    assert panel.get_log_count() == 0, f'Expected 0, got {panel.get_log_count()}'
    
    # Test 9: get_last_log après clear
    print('\nTest 9: get_last_log après clear')
    last = panel.get_last_log()
    print(f'✓ get_last_log après clear: {last}')
    assert last is None, f'Expected None, got {last}'
    
    # Test 10: Ajouter des logs après clear
    print('\nTest 10: Ajouter des logs après clear')
    panel.add_log('Premier log après clear', '🔄', 'info')
    print(f'✓ Log ajouté après clear, count={panel.get_log_count()}')
    assert panel.get_log_count() == 1, f'Expected 1, got {panel.get_log_count()}'
    
    # Test 11: Vérifier les différents niveaux
    print('\nTest 11: Vérifier les différents niveaux')
    panel.clear()
    panel.add_log('Info message', 'ℹ️', 'info')
    panel.add_log('Success message', '✅', 'success')
    panel.add_log('Warning message', '⚠️', 'warning')
    panel.add_log('Error message', '❌', 'error')
    logs = panel.get_logs()
    assert len(logs) == 4, f'Expected 4, got {len(logs)}'
    assert logs[0].level == 'info', f'Expected info, got {logs[0].level}'
    assert logs[1].level == 'success', f'Expected success, got {logs[1].level}'
    assert logs[2].level == 'warning', f'Expected warning, got {logs[2].level}'
    assert logs[3].level == 'error', f'Expected error, got {logs[3].level}'
    print(f'✓ Tous les niveaux fonctionnent correctement')
    
    print('\n✅ Tous les tests passent!')
    return True

if __name__ == "__main__":
    try:
        test_logs_panel()
        print("\n" + "="*50)
        print("SUCCESS: LogsPanel fonctionne correctement!")
        print("="*50)
    except AssertionError as e:
        print(f"\n❌ ERREUR: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"\n❌ ERREUR INATTENDUE: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
