<#
.SYNOPSIS
  Builds a fully self-contained, offline Windows executable of the
  Airport Cyber Resilience Simulator using PyInstaller.

.DESCRIPTION
  Bundles the Python runtime, all dependencies (FastAPI, uvicorn, websockets),
  the static frontend, vendored JS libraries, vendored fonts and the JSON data
  into a single AirportCyberSim.exe under .\dist. No Python, Docker or internet
  is required to RUN the result.

  Requires (build time only): Python 3.11+, pip, and PyInstaller
  (pip install pyinstaller).

.EXAMPLE
  .\build_exe.ps1
#>
[CmdletBinding()]
param(
    [switch]$OneDir  # build a folder instead of a single .exe (faster startup)
)

$ErrorActionPreference = 'Stop'
Set-Location -Path $PSScriptRoot

if (-not (Get-Command python -ErrorAction SilentlyContinue)) {
    Write-Host "Python not found on PATH." -ForegroundColor Red; exit 1
}
try { python -c "import PyInstaller" 2>$null } catch {}
if (-not $?) {
    Write-Host "PyInstaller not installed. Installing it now..." -ForegroundColor Yellow
    python -m pip install pyinstaller
}

$mode = if ($OneDir) { '--onedir' } else { '--onefile' }
$src = Join-Path $PSScriptRoot 'src'

Write-Host "Building AirportCyberSim.exe ($mode)..." -ForegroundColor Cyan

python -m PyInstaller `
    --noconfirm --clean $mode `
    --name AirportCyberSim `
    --distpath dist --workpath build --specpath build `
    --paths "$src" `
    --add-data "$src\static;static" `
    --add-data "$src\data;data" `
    --collect-all uvicorn `
    --collect-all websockets `
    --collect-submodules anyio `
    "$src\run.py"

if ($LASTEXITCODE -eq 0) {
    Write-Host ""
    Write-Host "Build complete." -ForegroundColor Green
    if ($OneDir) {
        Write-Host "Run: .\dist\AirportCyberSim\AirportCyberSim.exe"
    } else {
        Write-Host "Run: .\dist\AirportCyberSim.exe"
    }
} else {
    Write-Host "Build failed (exit $LASTEXITCODE)." -ForegroundColor Red
    exit $LASTEXITCODE
}
