#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Install AiSrt on Windows

.DESCRIPTION
    Copies aisrt.exe and appsettings.json to %LOCALAPPDATA%\AiSrt
    and adds the directory to the user PATH.
#>

$ErrorActionPreference = "Stop"

$installDir = Join-Path $env:LOCALAPPDATA "AiSrt"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

Write-Host ""
Write-Host "Installing AiSrt..." -ForegroundColor Cyan
Write-Host ""

# Create install directory
if (-not (Test-Path $installDir)) {
    New-Item -ItemType Directory -Force -Path $installDir | Out-Null
    Write-Host "  Created $installDir" -ForegroundColor Green
}

# Copy files
$exePath = Join-Path $scriptDir "aisrt.exe"
if (-not (Test-Path $exePath)) {
    Write-Error "aisrt.exe not found in $scriptDir"
    exit 1
}

Copy-Item $exePath (Join-Path $installDir "aisrt.exe") -Force
Write-Host "  Copied aisrt.exe" -ForegroundColor Green

$configPath = Join-Path $scriptDir "appsettings.json"
if (Test-Path $configPath) {
    $destConfig = Join-Path $installDir "appsettings.json"
    if (-not (Test-Path $destConfig)) {
        Copy-Item $configPath $destConfig
        Write-Host "  Copied appsettings.json" -ForegroundColor Green
    } else {
        Write-Host "  appsettings.json already exists, skipping (edit manually if needed)" -ForegroundColor Yellow
    }
}

# Add to PATH if not already present
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($userPath -notlike "*$installDir*") {
    [Environment]::SetEnvironmentVariable("PATH", "$userPath;$installDir", "User")
    Write-Host "  Added $installDir to user PATH" -ForegroundColor Green
    Write-Host "  (Restart your terminal for PATH changes to take effect)" -ForegroundColor Yellow
} else {
    Write-Host "  $installDir already in PATH" -ForegroundColor Green
}

Write-Host ""
Write-Host "AiSrt installed successfully!" -ForegroundColor Green
Write-Host "Run 'aisrt --help' to get started." -ForegroundColor Cyan
Write-Host ""
