$ErrorActionPreference = "Stop"

$source = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$packageScript = Join-Path $source "tools\exe\build-windows-exe-package.ps1"
$issPath = Join-Path $source "tools\exe\fashion-ai-design-installer.iss"
$packageJson = Get-Content (Join-Path $source "package.json") -Raw | ConvertFrom-Json
$version = $packageJson.version

& powershell -ExecutionPolicy Bypass -File $packageScript

$isccCandidates = @(
  "C:\Program Files (x86)\Inno Setup 6\ISCC.exe",
  "C:\Program Files\Inno Setup 6\ISCC.exe"
)

$iscc = $isccCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if (-not $iscc) {
  throw "Inno Setup 6 is not installed. Please install ISCC.exe, then rerun build-windows-installer.ps1."
}

& $iscc "/DMyAppVersion=$version" $issPath
if ($LASTEXITCODE -ne 0) {
  throw "ISCC failed with exit code $LASTEXITCODE"
}

Write-Host "[Done] Windows graphical installer created in delivery-build."
