# Build complet : publish + Inno Setup
#
# Pre-requis :
#   - .NET 8 SDK
#   - Inno Setup 6.x  (https://jrsoftware.org/isdl.php)
#
# Lancer depuis la racine du repo :
#   powershell -ExecutionPolicy Bypass -File installer\build-installer.ps1
#
# NB : ce fichier est volontairement sans accents pour rester compatible
# PowerShell 5.1 (qui lit les .ps1 sans BOM en Windows-1252, pas UTF-8).

param(
    [string]$ISCC = ""
)

$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot

# Resolution de ISCC.exe : argument explicite > emplacements connus > PATH.
function Find-Iscc {
    param([string]$Hint)

    if ($Hint -and (Test-Path $Hint)) { return $Hint }

    # Couvre les installs system-wide (Program Files) et user-mode
    # (%LocalAppData%\Programs, emplacement par defaut quand winget installe sans admin).
    $candidates = @(
        "C:\Program Files (x86)\Inno Setup 6\ISCC.exe",
        "C:\Program Files\Inno Setup 6\ISCC.exe",
        "$env:LOCALAPPDATA\Programs\Inno Setup 6\ISCC.exe",
        "C:\Program Files (x86)\Inno Setup 5\ISCC.exe",
        "C:\Program Files\Inno Setup 5\ISCC.exe",
        "$env:LOCALAPPDATA\Programs\Inno Setup 5\ISCC.exe"
    )
    foreach ($c in $candidates) {
        if (Test-Path $c) { return $c }
    }

    $cmd = Get-Command iscc -ErrorAction SilentlyContinue
    if ($cmd) { return $cmd.Source }

    return $null
}

Write-Host "==> dotnet publish PSLauncher.App (Release single-file)" -ForegroundColor Cyan
dotnet publish "$root\src\PSLauncher.App\PSLauncher.App.csproj" -c Release --nologo
if ($LASTEXITCODE -ne 0) { throw "publish PSLauncher.App failed" }

Write-Host "==> dotnet publish PSLauncher.Updater (Release single-file)" -ForegroundColor Cyan
dotnet publish "$root\src\PSLauncher.Updater\PSLauncher.Updater.csproj" -c Release --nologo
if ($LASTEXITCODE -ne 0) { throw "publish PSLauncher.Updater failed" }

$resolved = Find-Iscc $ISCC
if (-not $resolved) {
    Write-Host ""
    Write-Host "ERREUR : Inno Setup (ISCC.exe) introuvable." -ForegroundColor Red
    Write-Host ""
    Write-Host "Installe Inno Setup 6 depuis :" -ForegroundColor Yellow
    Write-Host "  https://jrsoftware.org/isdl.php" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Ou installation silencieuse via winget :" -ForegroundColor Yellow
    Write-Host "  winget install JRSoftware.InnoSetup" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Une fois installe, relance ce script. Si ISCC.exe est dans un emplacement" -ForegroundColor Yellow
    Write-Host "non standard, passe le chemin via -ISCC :" -ForegroundColor Yellow
    Write-Host "  .\installer\build-installer.ps1 -ISCC 'D:\Tools\Inno\ISCC.exe'" -ForegroundColor Yellow
    Write-Host ""
    throw "ISCC.exe introuvable. Inno Setup n'est pas installe sur cette machine."
}

Write-Host "==> Inno Setup compile (ISCC: $resolved)" -ForegroundColor Cyan
& $resolved "$PSScriptRoot\PSLauncher.iss"
if ($LASTEXITCODE -ne 0) { throw "ISCC failed" }

Write-Host ""
Write-Host "OK - installer disponible dans: $PSScriptRoot\output\" -ForegroundColor Green
Get-ChildItem "$PSScriptRoot\output" -Filter "*.exe" | ForEach-Object {
    Write-Host "  $($_.Name)  ($([math]::Round($_.Length / 1MB, 1)) Mo)"
}
