# Build archived release folders for prior Nexus builds (package only, no new build number).
param(
    [string]$OutputRoot = "",
    [string]$DownloadBaseUrl = "https://nexus.ninjakernen.info/downloads"
)

$ErrorActionPreference = "Stop"
$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$monorepoRoot = Split-Path -Parent $repoRoot
$distRoot = if ($OutputRoot) { $OutputRoot } else { Join-Path $repoRoot "dist\nexus-laptop-poc" }
$uploadRoot = Join-Path $distRoot "upload"

$archives = @(
    @{ Commit = "nexus-build-87"; NexusRootRel = ""; Version = "0.1.0-identity2"; Build = 87 }
    @{ Commit = "d1502a4"; NexusRootRel = "Ninja Nexus"; Version = "0.1.0-identity3"; Build = 88 }
)

foreach ($arc in $archives) {
    $releaseDir = Join-Path $uploadRoot "downloads\releases\$($arc.Version)"
    $zipPath = Join-Path $releaseDir "nexus-shell-poc.zip"
    if ((Test-Path $zipPath) -and (Test-Path (Join-Path $releaseDir "manifest.json"))) {
        Write-Host "Skip build $($arc.Build) - artifacts already at $releaseDir" -ForegroundColor DarkGray
        continue
    }

    $workDir = Join-Path $env:TEMP "nexus-hist-$($arc.Build)-$([Guid]::NewGuid().ToString('N'))"
    Write-Host "=== Archiving build $($arc.Build) ($($arc.Version)) from $($arc.Commit) ===" -ForegroundColor Cyan

    Push-Location $monorepoRoot
    try {
        $prevEap = $ErrorActionPreference
        $ErrorActionPreference = "Continue"
        git worktree add $workDir $arc.Commit --detach 2>&1 | Out-Null
        $ErrorActionPreference = $prevEap
        if ($LASTEXITCODE -ne 0) { throw "git worktree add failed for $($arc.Commit)" }

        $nexusRoot = if ($arc.NexusRootRel) { Join-Path $workDir $arc.NexusRootRel } else { $workDir }
        $buildPackage = Join-Path $nexusRoot "scripts\nexus-laptop-poc\build-package.ps1"
        if (-not (Test-Path $buildPackage)) {
            throw "build-package.ps1 not found at $buildPackage"
        }

        $arcDist = Join-Path $workDir "dist-build-$($arc.Build)"
        & $buildPackage -Version $arc.Version -Build $arc.Build -OutputRoot $arcDist -DownloadBaseUrl $DownloadBaseUrl
        if ($LASTEXITCODE -ne 0) { throw "build-package failed for build $($arc.Build)" }

        $srcRelease = Join-Path $arcDist "upload\downloads\releases\$($arc.Version)"
        if (-not (Test-Path $srcRelease)) { throw "Expected release dir missing: $srcRelease" }

        New-Item -ItemType Directory -Path $releaseDir -Force | Out-Null
        Get-ChildItem -LiteralPath $srcRelease -Force | Copy-Item -Destination $releaseDir -Recurse -Force
        if (-not (Test-Path (Join-Path $releaseDir "nexus-shell-poc.zip"))) {
            throw "Copy failed for build $($arc.Build) - zip missing in $releaseDir"
        }
        Write-Host "Copied build $($arc.Build) to $releaseDir" -ForegroundColor Green
    } finally {
        Pop-Location
        if (Test-Path $workDir) {
            $prevEap = $ErrorActionPreference
            $ErrorActionPreference = "Continue"
            git worktree remove $workDir --force 2>&1 | Out-Null
            $ErrorActionPreference = $prevEap
        }
    }
}

Write-Host "Historical release restore complete." -ForegroundColor Cyan
