# build_windows.ps1 - ONE-COMMAND build: daw_engine.exe + daw_vst_bridge.exe + Tauri (NSIS+MSI)
# Run on Windows:  powershell -ExecutionPolicy Bypass -File build_windows.ps1
# ASCII only (PowerShell 5.1 ANSI limitation).
$ErrorActionPreference = "Stop"
Set-Location (Join-Path $PSScriptRoot "..\..")   # repo root

Write-Host "== [0/7] Kill old sidecars (daw_engine, daw_vst_bridge) =="
Get-Process -Name "daw_engine" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Get-Process -Name "daw_vst_bridge" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds 500

Write-Host "== [1/7] Python dependencies =="
python -m pip install --upgrade pip
python -m pip install -r requirements.txt pyinstaller pywin32

Write-Host "== [2/7] Frontend bundle (app.jsx -> app.precompiled.js) =="
npm install
if (-not (Test-Path "node_modules\@babel\standalone")) { npm install @babel/standalone --no-audit --no-fund }
if (-not (Test-Path "node_modules\@babel\standalone")) { Write-Host "ERROR: @babel/standalone missing" -ForegroundColor Red; exit 1 }
node build.mjs

Write-Host "== [3/7] Build daw_engine.exe (PyInstaller ONEDIR) =="
pyinstaller engine.spec --clean --noconfirm
$specContent = Get-Content "engine.spec" -Raw -ErrorAction SilentlyContinue
if ($specContent -notmatch "collect_data_files\('app'") { Write-Host "ERROR: engine.spec too old" -ForegroundColor Red; exit 1 }
python tools\verify_bundle.py
if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: bundle missing assets" -ForegroundColor Red; exit 1 }

Write-Host "== [4/7] Copy engine onedir -> src-tauri\resources\daw_engine =="
if (-not (Test-Path "dist\daw_engine\daw_engine.exe")) { Write-Host "ERROR: onedir engine missing" -ForegroundColor Red; exit 1 }
if (Test-Path "src-tauri\resources\daw_engine") { Remove-Item -Recurse -Force "src-tauri\resources\daw_engine" }
New-Item -ItemType Directory -Force "src-tauri\resources\daw_engine" | Out-Null
Copy-Item "dist\daw_engine\*" "src-tauri\resources\daw_engine\" -Recurse -Force

Write-Host "== [5/7] Build native bridge (daw_vst_bridge.exe) =="
& (Join-Path $PSScriptRoot "build_native_bridge.ps1")
Set-Location (Join-Path $PSScriptRoot "..\..")   # restore CWD (child script must not leak it)
if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: native bridge build failed" -ForegroundColor Red; exit 1 }
if (-not (Test-Path "src-tauri\binaries\daw_vst_bridge-x86_64-pc-windows-msvc.exe")) {
  Write-Host "ERROR: bridge sidecar missing" -ForegroundColor Red; exit 1
}
if (-not (Test-Path "src-tauri\binaries\libfluidsynth-3.dll")) {
  Write-Host "ERROR: libfluidsynth-3.dll missing (bridge runtime DLL)" -ForegroundColor Red; exit 1
}

Write-Host "== [6/7] VC++ Redistributable for hooks.nsh =="
if (-not (Test-Path src-tauri\vc_redist.x64.exe)) {
  Invoke-WebRequest -Uri "https://aka.ms/vs/17/release/vc_redist.x64.exe" -OutFile src-tauri\vc_redist.x64.exe
}

Write-Host "== [7/7] Tauri build (NSIS + MSI) =="
if (-not (Test-Path "src-tauri\resources\daw_engine\daw_engine.exe")) { Write-Host "ERROR: engine resource missing" -ForegroundColor Red; exit 1 }
if (-not (Test-Path "src-tauri\resources\daw_engine\_internal")) { Write-Host "ERROR: engine _internal missing" -ForegroundColor Red; exit 1 }
if (-not (Test-Path "src-tauri\binaries\daw_vst_bridge-x86_64-pc-windows-msvc.exe")) { Write-Host "ERROR: bridge externalBin missing" -ForegroundColor Red; exit 1 }
python tools\verify_bundle.py --check-bridge
if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: bridge sidecar missing" -ForegroundColor Red; exit 1 }
npm install -D @tauri-apps/cli
# Frontend host: copy template -> src-tauri/ui/index.html (giu Tauri context,
# document.write UI engine; neu thieu -> window mat __TAURI__ -> bridge chet).
Copy-Item buildscriptsfrontend_host_index.html src-tauriuiindex.html -Force
if (-not (Test-Path src-tauriuiindex.html)) { Write-Host "ERROR: frontend host missing" -ForegroundColor Red; exit 1 }
npx tauri build

Write-Host ""
Write-Host "== DONE =="
Write-Host "  NSIS: src-tauri\target\release\bundle\nsis\SonicForgeDAW_1.0.0_x64-setup.exe"
Write-Host "  MSI : src-tauri\target\release\bundle\msi\SonicForgeDAW_1.0.0_x64_en-US.msi"
Write-Host "  Verify after install: %APPDATA%\SonicForgeDAW\logs\spawn.log exists=True for daw_engine AND daw_vst_bridge"
