<#
.SYNOPSIS
    Codex Launcher Windows Installer
.DESCRIPTION
    Installs Codex Launcher for the current user.
.NOTES
    Requires: Python 3.8+ (stdlib only, zero pip dependencies).
#>

param(
    [switch]$Uninstall
)

$ErrorActionPreference = 'Stop'
$BinDir = Join-Path $env:LOCALAPPDATA 'Programs\Codex-Launcher'
$StartMenu = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'

if ($Uninstall) {
    Write-Host 'Uninstalling Codex Launcher...' -ForegroundColor Yellow

    if (Test-Path $BinDir) {
        Remove-Item -Recurse -Force $BinDir
        Write-Host "  Removed $BinDir"
    }

    $shortcut = Join-Path $StartMenu 'Codex Launcher.lnk'
    if (Test-Path $shortcut) {
        Remove-Item -Force $shortcut
        Write-Host '  Removed Start Menu shortcut'
    }

    $userPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
    if ($userPath -like "*$BinDir*") {
        $newPath = ($userPath -split ';' | Where-Object { $_ -ne $BinDir }) -join ';'
        [Environment]::SetEnvironmentVariable('PATH', $newPath, 'User')
        Write-Host '  Removed from PATH'
    }

    Write-Host 'Uninstall complete.' -ForegroundColor Green
    return
}

Write-Host ''
Write-Host '  Codex Launcher - Windows Installer' -ForegroundColor Cyan
Write-Host '  ====================================' -ForegroundColor Cyan
Write-Host ''

# Check Python
$pythonExe = Get-Command python -ErrorAction SilentlyContinue
if (-not $pythonExe) {
    $pythonExe = Get-Command python3 -ErrorAction SilentlyContinue
}
if (-not $pythonExe) {
    Write-Host 'ERROR: Python not found. Install Python 3.8+ and add to PATH.' -ForegroundColor Red
    exit 1
}
Write-Host "  Python: $($pythonExe.Source)" -ForegroundColor Gray

# Create install directory
New-Item -ItemType Directory -Force -Path $BinDir | Out-Null

# Copy files
$srcDir = Join-Path $PSScriptRoot 'src'
$files = @(
    'translate-proxy.py',
    'codex-launcher-gui.py',
    'codex_launcher_lib.py',
    'cleanup-codex-stale.py'
)

foreach ($file in $files) {
    $src = Join-Path $srcDir $file
    if (Test-Path $src) {
        Copy-Item -Force $src $BinDir
        Write-Host "  Installed: $file" -ForegroundColor Green
    } else {
        Write-Host "  WARNING: $file not found in src/" -ForegroundColor Yellow
    }
}

# Create Start Menu shortcut
$WshShell = New-Object -ComObject WScript.Shell
$shortcutPath = Join-Path $StartMenu 'Codex Launcher.lnk'
$Shortcut = $WshShell.CreateShortcut($shortcutPath)

# Find pythonw.exe for no-console launch
$pythonw = Get-Command pythonw -ErrorAction SilentlyContinue
if (-not $pythonw) {
    $pythonDir = Split-Path $pythonExe.Source
    $pythonwCandidate = Join-Path $pythonDir 'pythonw.exe'
    if (Test-Path $pythonwCandidate) {
        $pythonw = $pythonwCandidate
    }
}

if ($pythonw) {
    $targetPath = if ($pythonw.Source) { $pythonw.Source } else { $pythonw }
} else {
    $targetPath = $pythonExe.Source
}
$Shortcut.TargetPath = $targetPath
$guiPath = Join-Path $BinDir 'codex-launcher-gui.py'
$Shortcut.Arguments = $guiPath
$Shortcut.WorkingDirectory = $BinDir
$Shortcut.Description = 'Launch Codex Desktop with any AI provider'
$Shortcut.Save()
Write-Host '  Created Start Menu shortcut' -ForegroundColor Green

# Add to PATH
$userPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
if ($userPath -notlike "*$BinDir*") {
    $newUserPath = $userPath + ';' + $BinDir
    [Environment]::SetEnvironmentVariable('PATH', $newUserPath, 'User')
    $env:PATH = $env:PATH + ';' + $BinDir
    Write-Host '  Added to user PATH' -ForegroundColor Green
}

# Verify
Write-Host ''
Write-Host '  Installation complete!' -ForegroundColor Cyan
Write-Host "  Install dir: $BinDir" -ForegroundColor Gray
Write-Host ''
Write-Host '  Launch options:' -ForegroundColor White
Write-Host '    Start Menu:  Codex Launcher' -ForegroundColor Gray
Write-Host '    Command:     codex-launcher-gui.py' -ForegroundColor Gray
Write-Host '    Uninstall:   powershell -File install.ps1 -Uninstall' -ForegroundColor Gray
Write-Host ''
