$url = 'https://nodejs.org/dist/v22.14.0/node-v22.14.0-win-x64.zip'
$extractTo = "$env:LOCALAPPDATA\nodejs-portable"
$zipPath = "$extractTo\node.zip"

if (!(Test-Path $extractTo)) { New-Item -ItemType Directory -Path $extractTo | Out-Null }

Write-Host "Downloading Node.js v22 LTS portable..."
Invoke-WebRequest -Uri $url -OutFile $zipPath -UseBasicParsing
Write-Host "Download complete ($([int]((Get-Item $zipPath).Length / 1MB)) MB). Extracting..."
Expand-Archive -Path $zipPath -DestinationPath $extractTo -Force
Write-Host "Extraction complete."

$nodePath = "$extractTo\node-v22.14.0-win-x64"
Write-Host "Node.js path: $nodePath"
& "$nodePath\node.exe" --version
& "$nodePath\npm.cmd" --version

# Add to current user PATH persistently
$userPath = [System.Environment]::GetEnvironmentVariable('PATH', 'User')
if ($userPath -notlike "*nodejs-portable*") {
    [System.Environment]::SetEnvironmentVariable('PATH', "$nodePath;$userPath", 'User')
    Write-Host "Added $nodePath to user PATH"
}

Write-Host "INSTALL_COMPLETE"
