# LLMHosts — Zero-Friction Installer for Windows
#
# Usage:
#   irm https://llmhosts.com/install.ps1 | iex
#
# What this does:
#   1. Installs UV (fast Python package manager) if not present
#   2. Installs llmhosts CLI via UV
#   3. Runs `llmhosts up` — which auto-installs Ollama + pulls a model for your GPU
#
# Requirements: PowerShell 5.1+ (pre-installed on Windows 10/11)
# Supported: Windows 10/11 (x86_64, ARM64)

$ErrorActionPreference = "Stop"

# ── Colors ────────────────────────────────────────────────────────────

function Write-Info  { Write-Host "  ▸ " -ForegroundColor Blue -NoNewline; Write-Host $args }
function Write-Ok    { Write-Host "  ✓ " -ForegroundColor Green -NoNewline; Write-Host $args }
function Write-Warn  { Write-Host "  ⚠ " -ForegroundColor Yellow -NoNewline; Write-Host $args }
function Write-Fail  { Write-Host "  ✗ " -ForegroundColor Red -NoNewline; Write-Host $args; exit 1 }
function Write-Step  { Write-Host "`n  ── Step $args ──" -ForegroundColor Cyan }

# ── Banner ────────────────────────────────────────────────────────────

Write-Host ""
Write-Host "  ╔═══════════════════════════════════════════╗" -ForegroundColor White
Write-Host "  ║           LLMHosts Installer              ║" -ForegroundColor White
Write-Host "  ║   Your hardware. Real AI infrastructure.  ║" -ForegroundColor White
Write-Host "  ╚═══════════════════════════════════════════╝" -ForegroundColor White
Write-Host ""

# ── Pre-flight ────────────────────────────────────────────────────────

$osArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
Write-Info "Detected: Windows $osArch"

# ── Step 1: UV ────────────────────────────────────────────────────────

Write-Step "1/3: Python toolchain (UV)"

$uvPath = Get-Command uv -ErrorAction SilentlyContinue
if ($uvPath) {
    $uvVersion = & uv --version 2>$null | Select-Object -First 1
    Write-Ok "UV already installed ($uvVersion)"
} else {
    Write-Info "Installing UV — fast Python package manager..."
    try {
        Invoke-RestMethod https://astral.sh/uv/install.ps1 | Invoke-Expression
    } catch {
        Write-Fail "UV installation failed. Try manually: https://docs.astral.sh/uv/getting-started/installation/"
    }

    # Refresh PATH for this session
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")

    $uvPath = Get-Command uv -ErrorAction SilentlyContinue
    if ($uvPath) {
        $uvVersion = & uv --version 2>$null | Select-Object -First 1
        Write-Ok "UV installed ($uvVersion)"
    } else {
        Write-Fail "UV installed but not found on PATH. Restart PowerShell and run: irm https://llmhosts.com/install.ps1 | iex"
    }
}

# ── Step 2: LLMHosts ─────────────────────────────────────────────────

Write-Step "2/3: LLMHosts CLI"

$llmhostsPath = Get-Command llmhosts -ErrorAction SilentlyContinue
if ($llmhostsPath) {
    $currentVersion = & llmhosts --version 2>$null
    Write-Ok "llmhosts already installed ($currentVersion)"
    Write-Info "Upgrading to latest..."
    & uv tool install --upgrade llmhosts 2>$null
} else {
    Write-Info "Installing llmhosts..."
    & uv tool install llmhosts
}

# Refresh PATH again
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")

$llmhostsPath = Get-Command llmhosts -ErrorAction SilentlyContinue
if (-not $llmhostsPath) {
    Write-Fail "llmhosts installed but not found on PATH. Restart PowerShell and run: llmhosts up"
}

$installedVersion = & llmhosts --version 2>$null
Write-Ok "llmhosts ready ($installedVersion)"

# ── Step 3: Launch ────────────────────────────────────────────────────

Write-Step "3/3: Infrastructure Autopilot"

Write-Info "Starting llmhosts up — this will:"
Write-Info "  • Detect your GPU and hardware"
Write-Info "  • Install Ollama (if needed)"
Write-Info "  • Pull the best AI model for your system"
Write-Info "  • Start the proxy and connect to your account"
Write-Host ""

& llmhosts up
