# Build NexusSetup.exe — one-click installer with embedded POC package + local login
param(
  [string]$Version = "0.1.0-poc1",
  [int]$Build = 1,
  [string]$OutputRoot = "",
  [string]$DownloadBaseUrl = "https://nexus.ninjakernen.info/downloads"
)

$ErrorActionPreference = "Stop"
$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$distRoot = if ($OutputRoot) { $OutputRoot } else { Join-Path $repoRoot "dist\nexus-laptop-poc" }
$installerProject = Join-Path $repoRoot "source\nexus-installer\NexusInstaller.csproj"
$payloadDir = Join-Path $repoRoot "source\nexus-installer\Resources"
$payloadZip = Join-Path $payloadDir "payload.zip"
$outDir = Join-Path $distRoot "installer"

Write-Host "=== Nexus Setup EXE Build ===" -ForegroundColor Cyan

Write-Host "Step 1/3: Building POC package..." -ForegroundColor Cyan
$package = & (Join-Path $PSScriptRoot "build-package.ps1") -Version $Version -Build $Build -OutputRoot $distRoot -DownloadBaseUrl $DownloadBaseUrl
if (-not $package.ZipPath -or -not (Test-Path $package.ZipPath)) {
  throw "Package build did not produce ZIP."
}

Write-Host "Step 2/3: Embedding payload..." -ForegroundColor Cyan
New-Item -ItemType Directory -Path $payloadDir -Force | Out-Null
Copy-Item -LiteralPath $package.ZipPath -Destination $payloadZip -Force

Write-Host "Step 3/3: Publishing NexusSetup.exe (self-contained win-x64)..." -ForegroundColor Cyan
if (Test-Path $outDir) { Remove-Item -LiteralPath $outDir -Recurse -Force }
New-Item -ItemType Directory -Path $outDir -Force | Out-Null

dotnet publish $installerProject `
  -c Release -r win-x64 --self-contained true `
  -p:PublishSingleFile=true `
  -p:IncludeNativeLibrariesForSelfExtract=true `
  -p:EnableCompressionInSingleFile=true `
  -o $outDir
if ($LASTEXITCODE -ne 0) { throw "Installer publish failed." }

$setupExe = Join-Path $outDir "NexusSetup.exe"
if (-not (Test-Path $setupExe)) { throw "NexusSetup.exe not found after publish." }

Copy-Item -LiteralPath $setupExe -Destination (Join-Path $distRoot "NexusSetup.exe") -Force

$uploadRoot = Join-Path $distRoot "upload"
$releaseDir = Join-Path $uploadRoot "downloads\releases\$Version"
$latestPath = Join-Path $uploadRoot "downloads\latest.json"
$installerHash = (Get-FileHash -LiteralPath $setupExe -Algorithm SHA256).Hash.ToLowerInvariant()
$installerSize = (Get-Item -LiteralPath $setupExe).Length

Copy-Item -LiteralPath $setupExe -Destination (Join-Path $uploadRoot "downloads\NexusSetup.exe") -Force
Copy-Item -LiteralPath $setupExe -Destination (Join-Path $releaseDir "NexusSetup.exe") -Force

function Write-Utf8NoBom([string]$Path, [string]$Content) {
  [System.IO.File]::WriteAllText($Path, $Content, (New-Object System.Text.UTF8Encoding $false))
}

if (Test-Path $latestPath) {
  $manifest = Get-Content $latestPath -Raw | ConvertFrom-Json
  $manifest | Add-Member -NotePropertyName installerUrl -NotePropertyValue "$DownloadBaseUrl/NexusSetup.exe" -Force
  $manifest | Add-Member -NotePropertyName installerSha256 -NotePropertyValue $installerHash -Force
  $manifest | Add-Member -NotePropertyName installerSizeBytes -NotePropertyValue $installerSize -Force
  $json = ($manifest | ConvertTo-Json -Depth 6 -Compress)
  Write-Utf8NoBom $latestPath $json
  Write-Utf8NoBom (Join-Path $distRoot "latest.json") $json
  Write-Utf8NoBom (Join-Path $releaseDir "manifest.json") $json
}

Write-Host ""
Write-Host "Installer:  $setupExe" -ForegroundColor Green
Write-Host "Copy also:  $(Join-Path $distRoot 'NexusSetup.exe')" -ForegroundColor Green
Write-Host ""
Write-Host "Brug:" -ForegroundColor Yellow
Write-Host "  1. Dobbeltklik NexusSetup.exe"
Write-Host "  2. Klik 'Installér' (standard: C:\NinjaNexusPOC)"
Write-Host "  3. Klik 'Start Ninja Nexus'"
Write-Host "  4. Log ind: user@nexus.local / NexusUser1!"
Write-Host ""

return [pscustomobject]@{
  SetupExe        = $setupExe
  SetupDistPath   = Join-Path $distRoot "NexusSetup.exe"
  UploadPath      = Join-Path $uploadRoot "downloads\NexusSetup.exe"
  InstallerSha256 = $installerHash
  InstallerSize   = $installerSize
  ZipPath         = $package.ZipPath
  Version         = $Version
  Build           = $Build
}
