#!/usr/bin/env bash
# LLMHosts — Zero-Friction Installer for Linux & macOS
#
# Usage:
#   curl -fsSL https://llmhosts.com/install.sh | bash
#
# 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: curl (pre-installed on most systems)
# Supported: Linux (x86_64, aarch64), macOS (Intel, Apple Silicon)

# Wrap in main() to prevent partial-download execution.
# If the connection drops mid-download, bash won't execute a half-script.
main() {

set -euo pipefail

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

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color

info()  { printf "${BLUE}▸${NC} %s\n" "$*"; }
ok()    { printf "${GREEN}✓${NC} %s\n" "$*"; }
warn()  { printf "${YELLOW}⚠${NC} %s\n" "$*"; }
fail()  { printf "${RED}✗${NC} %s\n" "$*"; exit 1; }
step()  { printf "\n${CYAN}${BOLD}── Step %s ──${NC}\n" "$*"; }

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

printf "\n${BOLD}"
printf "  ╔═══════════════════════════════════════════╗\n"
printf "  ║           LLMHosts Installer              ║\n"
printf "  ║   Your hardware. Real AI infrastructure.  ║\n"
printf "  ╚═══════════════════════════════════════════╝${NC}\n\n"

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

OS="$(uname -s)"
ARCH="$(uname -m)"
info "Detected: ${OS} ${ARCH}"

case "${OS}" in
    Linux|Darwin) ;;
    *) fail "Unsupported OS: ${OS}. Use install.ps1 for Windows." ;;
esac

command -v curl >/dev/null 2>&1 || fail "curl is required but not found. Install it first."

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

step "1/3: Python toolchain (UV)"

if command -v uv >/dev/null 2>&1; then
    UV_VERSION=$(uv --version 2>/dev/null | head -1)
    ok "UV already installed (${UV_VERSION})"
else
    info "Installing UV — fast Python package manager..."
    curl -LsSf https://astral.sh/uv/install.sh | sh

    # Source the env so uv is available in this session
    export PATH="${HOME}/.local/bin:${HOME}/.cargo/bin:${PATH}"

    if command -v uv >/dev/null 2>&1; then
        ok "UV installed ($(uv --version 2>/dev/null | head -1))"
    else
        fail "UV installation failed. Try manually: https://docs.astral.sh/uv/getting-started/installation/"
    fi
fi

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

step "2/3: LLMHosts CLI"

if command -v llmhosts >/dev/null 2>&1; then
    CURRENT_VERSION=$(llmhosts --version 2>/dev/null || echo "unknown")
    ok "llmhosts already installed (${CURRENT_VERSION})"
    info "Upgrading to latest..."
    uv tool install --upgrade llmhosts 2>/dev/null || uv tool upgrade llmhosts 2>/dev/null || true
else
    info "Installing llmhosts..."
    uv tool install llmhosts
fi

# Verify it's on PATH
if ! command -v llmhosts >/dev/null 2>&1; then
    # UV tool bin might not be on PATH yet
    export PATH="${HOME}/.local/bin:${PATH}"
    if ! command -v llmhosts >/dev/null 2>&1; then
        fail "llmhosts installed but not found on PATH. Restart your terminal and run: llmhosts up"
    fi
fi

ok "llmhosts ready ($(llmhosts --version 2>/dev/null || echo 'installed'))"

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

step "3/3: Infrastructure Autopilot"

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

exec llmhosts up

}

main "$@"
