"""
Point d'entrée principal pour l'interface graphique Web Sentinel.
Ce script peut être utilisé directement ou compilé avec PyInstaller.
Supporte les thèmes de couleur via la variable WS_GUI_THEME.
"""

import sys
import os
from pathlib import Path

# Fix encodage UTF-8 pour Windows (éviter UnicodeEncodeError avec emojis)
if sys.platform == 'win32':
    if hasattr(sys.stdout, 'reconfigure'):
        if sys.stdout.encoding != 'utf-8':
            sys.stdout.reconfigure(encoding='utf-8', errors='replace')
        if sys.stderr.encoding != 'utf-8':
            sys.stderr.reconfigure(encoding='utf-8', errors='replace')

# Ajouter le dossier racine au path pour les imports AVANT les imports web_sentinel
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from web_sentinel.gui.i18n import localization

def main():
    """Point d'entrée principal de l'application GUI avec thème de couleur."""
    try:
        # Importer le gestionnaire de données pour récupérer le thème
        from web_sentinel.gui.themes_gui.shared import get_data_manager
        from web_sentinel.gui.theme_applicator import ThemeApplicator
        
        data_manager = get_data_manager()
        theme = data_manager.get_gui_theme()
        
        print(f"[Web Sentinel] Application du thème de couleur: {theme}")
        
        # Toujours utiliser l'interface complète (moderne ou classique)
        try:
            from web_sentinel.gui.modern_tabbed_window import ModernWebSentinelGUI
            app = ModernWebSentinelGUI(initial_theme=theme)
            app.run()
            
        except ImportError as e:
            # Fallback vers l'ancienne interface
            from web_sentinel.gui.main_window import WebSentinelGUI
            print("[Web Sentinel] Utilisation du GUI classique")
            app = WebSentinelGUI()
            
            if theme != "classic":
                ThemeApplicator.apply_theme(app.root, theme)
            
            app.run()
        
    except ImportError as e:
        print(localization.t("messages.gui.import_error", error=str(e)))
        print(localization.t("messages.gui.install_hint"))
        print(localization.t("messages.gui.install_command"))
        
        # Fallback vers l'ancienne interface si nécessaire
        try:
            from web_sentinel.gui.main_window import WebSentinelGUI
            print(localization.t("messages.gui.fallback_classic"))
            app = WebSentinelGUI()
            app.run()
        except:
            sys.exit(1)
    
    except Exception as e:
        print(localization.t("messages.gui.unexpected_error", error=str(e)))
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()
