#!/usr/bin/env python3
"""
Script de build moderne pour Web Sentinel
========================================

Construit l'exécutable avec PyInstaller en utilisant la nouvelle structure.
"""

import PyInstaller.__main__
import shutil
import sys
from pathlib import Path

def build_executable():
    """Construire l'exécutable Web Sentinel."""
    
    # Chemins
    root_dir = Path(__file__).parent.parent.parent
    src_dir = root_dir / "src"
    assets_dir = root_dir / "assets"
    config_dir = root_dir / "config"
    
    # Arguments PyInstaller
    args = [
        str(root_dir / "web_sentinel_app.py"),
        "--name=WebSentinel",
        "--onefile",
        "--windowed",
        f"--add-data={assets_dir};assets",
        f"--add-data={config_dir};config", 
        f"--add-data={src_dir};src",
        "--icon=assets/images/web-sentinel.png" if (assets_dir / "images" / "web-sentinel.png").exists() else "",
        "--clean",
        "--noconfirm",
        f"--distpath={root_dir / 'dist'}",
        f"--workpath={root_dir / 'build'}",
        f"--specpath={root_dir}",
        "--hidden-import=tkinter",
        "--hidden-import=PIL",
        "--hidden-import=requests",
        "--hidden-import=cryptography",
        "--collect-all=web_sentinel"
    ]
    
    # Supprimer les arguments vides
    args = [arg for arg in args if arg]
    
    print("🔨 Construction de l'exécutable Web Sentinel...")
    print(f"📂 Répertoire source: {src_dir}")
    print(f"📂 Assets: {assets_dir}")
    
    try:
        PyInstaller.__main__.run(args)
        print("✅ Build terminé avec succès!")
        print(f"📦 Exécutable disponible dans: {root_dir / 'dist'}")
        
    except Exception as e:
        print(f"❌ Erreur lors du build: {e}")
        sys.exit(1)

if __name__ == "__main__":
    build_executable()