param(
  [Parameter(Mandatory = $true)][string]$OutputArchive,
  [Parameter(Mandatory = $true)][string]$ManifestPath
)

$ErrorActionPreference = "Stop"
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path
$project = Join-Path $repoRoot "companion\src\Lumi.Companion.App\Lumi.Companion.App.csproj"
$outputArchive = [System.IO.Path]::GetFullPath($OutputArchive)
$manifest = [System.IO.Path]::GetFullPath($ManifestPath)
$workRoot = Join-Path ([System.IO.Path]::GetDirectoryName($outputArchive)) "work"
$publishRoot = Join-Path $workRoot "publish"
$stageRoot = Join-Path $workRoot "package"

if (!(Test-Path $manifest)) { throw "The development build manifest is missing." }
& (Join-Path $PSScriptRoot "generate-companion-icon.ps1")
Remove-Item $workRoot -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path $publishRoot, $stageRoot | Out-Null
New-Item -ItemType Directory -Force -Path ([System.IO.Path]::GetDirectoryName($outputArchive)) | Out-Null

$localDotnet = Join-Path $HOME ".dotnet-sdk\dotnet.exe"
$dotnet = if (Test-Path $localDotnet) { $localDotnet } else { (Get-Command dotnet.exe -ErrorAction Stop).Source }
$arguments = @(
  "publish", "`"$project`"",
  "-c", "Release",
  "-r", "win-x64",
  "--self-contained", "true",
  "--disable-build-servers",
  "-o", "`"$publishRoot`"",
  "-p:PublishSingleFile=true",
  "-p:IncludeNativeLibrariesForSelfExtract=true",
  "-p:DebugType=None"
)
$published = Start-Process -FilePath $dotnet -ArgumentList $arguments -NoNewWindow -Wait -PassThru
if ($published.ExitCode -ne 0) { throw "Companion development publish failed." }

$app = Join-Path $publishRoot "Lumi.Companion.App.exe"
if (!(Test-Path $app)) { throw "The Companion development publish did not create Lumi.Companion.App.exe." }
Copy-Item $app $stageRoot -Force
$componentsSource = Join-Path $publishRoot "components"
if (Test-Path $componentsSource) { Copy-Item $componentsSource (Join-Path $stageRoot "components") -Recurse -Force }

$legalSource = Join-Path $repoRoot "companion\legal"
$legalTarget = Join-Path $stageRoot "legal"
if (Test-Path $legalSource) {
  New-Item -ItemType Directory -Force -Path $legalTarget | Out-Null
  foreach ($name in @("LUMI-COMPANION-LICENCE.txt", "PRIVACY-NOTICE.txt", "THIRD-PARTY-NOTICES.txt", "LEGAL-README.txt")) {
    $source = Join-Path $legalSource $name
    if (Test-Path $source) { Copy-Item $source (Join-Path $legalTarget $name) -Force }
  }
  $thirdParty = Join-Path $legalSource "third-party"
  if (Test-Path $thirdParty) { Copy-Item $thirdParty (Join-Path $legalTarget "third-party") -Recurse -Force }
}
Copy-Item $manifest (Join-Path $stageRoot ".lumi-dev-build.json") -Force
Remove-Item $outputArchive -Force -ErrorAction SilentlyContinue
$archiveItems = Get-ChildItem $stageRoot -Force | Select-Object -ExpandProperty FullName
$archiveCreated = $false
for ($attempt = 1; $attempt -le 20; $attempt++) {
  try {
    Compress-Archive -Path $archiveItems -DestinationPath $outputArchive -CompressionLevel Optimal -ErrorAction Stop
    $archiveCreated = $true
    break
  }
  catch {
    Remove-Item $outputArchive -Force -ErrorAction SilentlyContinue
    if ($attempt -eq 20) { throw }
    Start-Sleep -Milliseconds 250
  }
}
if (-not $archiveCreated) { throw "The Companion development archive could not be created." }
Remove-Item $workRoot -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Created localhost development update: $outputArchive"
