<#
.SYNOPSIS
  Cross-compile the FlatRender Node Agent to a Windows .exe.

.DESCRIPTION
  Produces dist\flatrender-node-agent.exe and stages agent.env.example + the
  deploy scripts alongside it, ready to copy to a render node.

  Requires Go 1.25+ installed locally (works on Windows, macOS, or Linux).

.EXAMPLE
  .\build-windows.ps1
#>
param(
  [string]$OutDir = (Join-Path $PSScriptRoot "dist")
)

$ErrorActionPreference = "Stop"
$agentRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path

New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
$exe = Join-Path $OutDir "flatrender-node-agent.exe"

Write-Host "Building Windows binary from $agentRoot ..."
$env:GOOS = "windows"
$env:GOARCH = "amd64"
$env:CGO_ENABLED = "0"
Push-Location $agentRoot
try {
  & go build -trimpath -ldflags="-s -w" -o $exe ./cmd/agent
  if ($LASTEXITCODE -ne 0) { throw "go build failed ($LASTEXITCODE)" }
} finally {
  Pop-Location
}

# Stage the deploy kit next to the exe
Copy-Item (Join-Path $PSScriptRoot "agent.env.example")            $OutDir -Force
Copy-Item (Join-Path $PSScriptRoot "install-service.ps1")         $OutDir -Force
Copy-Item (Join-Path $PSScriptRoot "uninstall-service.ps1")       $OutDir -Force
Copy-Item (Join-Path $PSScriptRoot "setup-wireguard.ps1")         $OutDir -Force
Copy-Item (Join-Path $PSScriptRoot "wireguard-node.conf.template") $OutDir -Force
Copy-Item (Join-Path $PSScriptRoot "README.md")                   $OutDir -Force

Write-Host ""
Write-Host "✓ Built: $exe" -ForegroundColor Green
Write-Host "  Deploy kit staged in: $OutDir"
Write-Host "  Copy that folder to each render node, then follow README.md."
