param(
    [Parameter(Mandatory = $true)][ValidateSet('baseline', 'reference')][string]$Copy,
    [string]$AllowedHosts = ''
)
# Agent S: start PatientPortal from a scan copy in Production on 127.0.0.1:16310, record the pid.
$ErrorActionPreference = 'Stop'
$scan = '<scratch>\sec\scan'
$appDir = Join-Path $scan "$Copy\PatientPortal\src\PatientPortal.Api"
$outDir = Join-Path $scan "out\zap\$Copy"
New-Item -ItemType Directory -Force $outDir | Out-Null
$env:ASPNETCORE_ENVIRONMENT = 'Production'
$env:ASPNETCORE_URLS = 'http://127.0.0.1:16310'
$env:Jwt__Key = 'zap-scan-test-signing-key-not-a-secret-7f3c9a21d0e4b8'
if ($AllowedHosts -ne '') { $env:AllowedHosts = $AllowedHosts } else { [Environment]::SetEnvironmentVariable('AllowedHosts', $null, 'Process') }
$line = "cd $appDir; ASPNETCORE_ENVIRONMENT=Production ASPNETCORE_URLS=http://127.0.0.1:16310 Jwt__Key=zap-scan-test-signing-key-not-a-secret-7f3c9a21d0e4b8"
if ($AllowedHosts -ne '') { $line += " AllowedHosts=$AllowedHosts" }
$line += " dotnet bin\Debug\net10.0\PatientPortal.Api.dll"
$proc = Start-Process -FilePath 'dotnet' -ArgumentList 'bin\Debug\net10.0\PatientPortal.Api.dll' -WorkingDirectory $appDir -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $outDir 'app-stdout.txt') -RedirectStandardError (Join-Path $outDir 'app-stderr.txt')
Set-Content -Path (Join-Path $outDir 'app.pid') -Value $proc.Id -Encoding ascii
Add-Content -Path (Join-Path $outDir 'start-line.txt') -Value ("{0} pid={1} : {2}" -f (Get-Date -Format 'HH:mm:ss'), $proc.Id, $line) -Encoding ascii
Write-Output ("started pid {0}" -f $proc.Id)
