#requires -Version 5.0
# AyaanScript+ Windows post-install.
#
# The MSI has already dropped ayaan.exe + aspkg.exe into
#   %LOCALAPPDATA%\Programs\AyaanScriptPlus\
# This script does the last-mile setup:
#   1. Add that dir to the user's PATH (if not already there)
#   2. Show a welcome banner with next steps

$ErrorActionPreference = 'SilentlyContinue'

function Write-Banner {
    Write-Host ""
    Write-Host "+============================================+" -ForegroundColor Cyan
    Write-Host "|         AyaanScript+ Installer             |" -ForegroundColor Yellow
    Write-Host "+============================================+" -ForegroundColor Cyan
    Write-Host ""
}

function Add-To-Path {
    $installDir = Join-Path $env:LOCALAPPDATA "Programs\AyaanScriptPlus"

    Write-Host "[1/2] Verifying binaries..." -ForegroundColor Cyan
    $ayaanExe = Join-Path $installDir "ayaan.exe"
    $aspkgExe = Join-Path $installDir "aspkg.exe"
    if ((Test-Path $ayaanExe) -and (Test-Path $aspkgExe)) {
        Write-Host "      ayaan.exe  ($((Get-Item $ayaanExe).Length / 1MB -as [int]) MB)" -ForegroundColor Green
        Write-Host "      aspkg.exe ($((Get-Item $aspkgExe).Length / 1MB -as [int]) MB)" -ForegroundColor Green
    } else {
        Write-Host "      ERROR: binaries not found at $installDir" -ForegroundColor Red
        exit 1
    }

    Write-Host ""
    Write-Host "[2/2] Updating PATH..." -ForegroundColor Cyan
    $userPath = [Environment]::GetEnvironmentVariable("Path", "User")
    if ($userPath -like "*$installDir*") {
        Write-Host "      Already on PATH." -ForegroundColor Green
    } else {
        $newPath = if ([string]::IsNullOrEmpty($userPath)) { $installDir } else { "$installDir;$userPath" }
        [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
        Write-Host "      Added $installDir to your user PATH." -ForegroundColor Green
        Write-Host "      Open a NEW terminal to pick it up." -ForegroundColor Yellow
    }
}

function Show-Done {
    Write-Host ""
    Write-Host "+============================================+" -ForegroundColor Green
    Write-Host "|          Installation Complete!            |" -ForegroundColor Green
    Write-Host "+============================================+" -ForegroundColor Green
    Write-Host ""
    Write-Host "AyaanScript+ v2.0.0 is installed." -ForegroundColor White
    Write-Host ""
    Write-Host "Try it (open a NEW PowerShell window first):" -ForegroundColor Cyan
    Write-Host "    ayaan                       " -NoNewline; Write-Host "(starts the REPL)" -ForegroundColor Gray
    Write-Host "    ayaan myfile.ayaan          " -NoNewline; Write-Host "(runs a script)" -ForegroundColor Gray
    Write-Host "    ayaan help                  " -NoNewline; Write-Host "(interactive tutorial)" -ForegroundColor Gray
    Write-Host "    aspkg help                  " -NoNewline; Write-Host "(package manager tour)" -ForegroundColor Gray
    Write-Host "    aspkg install <name>        " -NoNewline; Write-Host "(install a package)" -ForegroundColor Gray
    Write-Host ""
}

Write-Banner
Add-To-Path
Show-Done
