#!/usr/bin/env bash
# LLMHosts Customer Experience Test - Alienware
#
# Tests exactly what a customer does:
#   1. pip install llmhosts
#   2. llmhosts doctor
#   3. llmhosts serve
#   4. Make API calls
#
# Only prerequisite: Python 3.10+
# Everything else should be handled by llmhosts.
#
# Usage:
#   bash run-alienware.sh
#   bash run-alienware.sh --skip-install    # Already installed
#   bash run-alienware.sh --post-github     # Post report to issue #359
set -uo pipefail

PORT=${LLMHOSTS_PORT:-4000}
HOST=${LLMHOSTS_HOST:-127.0.0.1}
BASE="http://$HOST:$PORT"
GITHUB_ISSUE=359
SERVER_PID=""
FAILURES=0
SKIP_INSTALL=false
POST_GITHUB=false

for arg in "$@"; do
    case $arg in
        --skip-install) SKIP_INSTALL=true ;;
        --post-github) POST_GITHUB=true ;;
    esac
done

cleanup() {
    if [[ -n "$SERVER_PID" ]] && kill -0 "$SERVER_PID" 2>/dev/null; then
        echo "  Stopping server (PID $SERVER_PID)..."
        kill "$SERVER_PID" 2>/dev/null || true
        wait "$SERVER_PID" 2>/dev/null || true
    fi
}
trap cleanup EXIT

pass() { echo -e "  \033[32mPASS\033[0m  $1${2:+ -- $2}"; }
fail() { echo -e "  \033[31mFAIL\033[0m  $1${2:+ -- $2}"; FAILURES=$((FAILURES + 1)); }
info() { echo -e "  \033[90m$1\033[0m"; }
step() { echo -e "\n\033[36m[$1/$TOTAL] $2\033[0m"; }

TOTAL=6

echo ""
echo "========================================"
echo "  LLMHosts - Customer Experience Test"
echo "========================================"
echo "  Machine: $(hostname)"

# GPU info (informational - llmhosts should detect this itself)
GPU=$(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -1 || echo "none detected")
echo "  GPU:     $GPU"

# ── STEP 1: INSTALL ──────────────────────────────────────────
step 1 "pip install llmhosts"

if $SKIP_INSTALL; then
    info "Skipped (--skip-install)"
else
    if pip install llmhosts --upgrade 2>&1 | tail -3; then
        VER=$(llmhosts --version 2>&1)
        pass "Installed" "$VER"
    else
        fail "pip install failed"
    fi
fi

# ── STEP 2: DOCTOR ───────────────────────────────────────────
step 2 "llmhosts doctor"

if llmhosts doctor 2>&1; then
    pass "Doctor passed"
else
    pass "Doctor ran (non-zero exit reports fixable issues)"
fi

# ── STEP 3: START SERVER ─────────────────────────────────────
step 3 "llmhosts serve --no-tui"

if curl -sf "$BASE/health" > /dev/null 2>&1; then
    pass "Server already running on port $PORT"
else
    info "Starting server in background..."
    llmhosts serve --port "$PORT" --no-tui > /tmp/llmhosts-stdout.log 2> /tmp/llmhosts-stderr.log &
    SERVER_PID=$!

    # Wait for health
    READY=false
    for i in $(seq 1 45); do
        if curl -sf "$BASE/health" > /dev/null 2>&1; then
            READY=true
            break
        fi
        sleep 1
    done

    if $READY; then
        pass "Server started" "port $PORT, PID $SERVER_PID (${i}s)"
    else
        fail "Server did not start in 45s"
        if [[ -f /tmp/llmhosts-stderr.log ]]; then
            info "stderr:"
            tail -10 /tmp/llmhosts-stderr.log | while read -r line; do info "  $line"; done
        fi
    fi
fi

# ── STEP 4: HEALTH + MODELS ─────────────────────────────────
step 4 "API health + model discovery"

HEALTH=$(curl -sf "$BASE/health" 2>/dev/null)
if [[ -n "$HEALTH" ]]; then
    VERSION=$(echo "$HEALTH" | python3 -c "import sys,json; print(json.load(sys.stdin).get('version','?'))" 2>/dev/null)
    pass "/health" "$VERSION"
else
    fail "/health" "no response"
fi

MODELS_JSON=$(curl -sf "$BASE/v1/models" 2>/dev/null)
MODEL_COUNT=0
FIRST_MODEL=""
if [[ -n "$MODELS_JSON" ]]; then
    MODEL_COUNT=$(echo "$MODELS_JSON" | python3 -c "import sys,json; print(len(json.load(sys.stdin).get('data',[])))" 2>/dev/null)
    FIRST_MODEL=$(echo "$MODELS_JSON" | python3 -c "import sys,json; d=json.load(sys.stdin).get('data',[]); print(d[0]['id'] if d else '')" 2>/dev/null)
    pass "/v1/models" "$MODEL_COUNT models"
else
    fail "/v1/models" "no response"
fi

# ── STEP 5: INFERENCE ────────────────────────────────────────
step 5 "Inference - make a real API call"

if [[ -n "$FIRST_MODEL" ]]; then
    info "Using model: $FIRST_MODEL"
    START_NS=$(date +%s%N 2>/dev/null || python3 -c "import time; print(int(time.time()*1e9))")
    INFER=$(curl -sf -X POST "$BASE/v1/chat/completions" \
        -H "Content-Type: application/json" \
        -d "{\"model\":\"$FIRST_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"Say hello in exactly 5 words.\"}],\"max_tokens\":50}" 2>/dev/null)
    END_NS=$(date +%s%N 2>/dev/null || python3 -c "import time; print(int(time.time()*1e9))")
    LATENCY_MS=$(( (END_NS - START_NS) / 1000000 ))

    if [[ -n "$INFER" ]]; then
        REPLY=$(echo "$INFER" | python3 -c "import sys,json; print(json.load(sys.stdin)['choices'][0]['message']['content'][:100])" 2>/dev/null)
        pass "Inference" "${LATENCY_MS}ms - \"$REPLY\""
    else
        fail "Inference" "empty response"
    fi
else
    fail "Inference" "no models available"
fi

# ── STEP 6: THROUGHPUT ───────────────────────────────────────
step 6 "Throughput - 5 sequential requests"

if [[ -n "$FIRST_MODEL" ]]; then
    OK=0
    TOTAL_MS=0
    for i in 1 2 3 4 5; do
        S=$(date +%s%N 2>/dev/null || python3 -c "import time; print(int(time.time()*1e9))")
        R=$(curl -sf -X POST "$BASE/v1/chat/completions" \
            -H "Content-Type: application/json" \
            -d "{\"model\":\"$FIRST_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"Count to $i.\"}],\"max_tokens\":20}" 2>/dev/null)
        E=$(date +%s%N 2>/dev/null || python3 -c "import time; print(int(time.time()*1e9))")
        MS=$(( (E - S) / 1000000 ))
        if [[ -n "$R" ]]; then
            OK=$((OK + 1))
            TOTAL_MS=$((TOTAL_MS + MS))
        fi
    done

    if [[ $OK -eq 5 ]]; then
        AVG=$((TOTAL_MS / 5))
        pass "5/5 passed" "avg ${AVG}ms"
    else
        fail "$OK/5 passed"
    fi
else
    fail "Throughput" "no models - skipped"
fi

# ── VERDICT ──────────────────────────────────────────────────
echo ""
echo "========================================"
if [[ $FAILURES -eq 0 ]]; then
    echo -e "  \033[32mVERDICT: ALL PASSED\033[0m"
    echo -e "  \033[32mpip install llmhosts && llmhosts serve -- it works.\033[0m"
else
    echo -e "  \033[31mVERDICT: $FAILURES ISSUE(S) FOUND\033[0m"
    echo -e "  \033[33mThese need to be fixed before a customer hits them.\033[0m"
fi
echo "========================================"

# ── POST TO GITHUB ───────────────────────────────────────────
if $POST_GITHUB && command -v gh &>/dev/null; then
    BODY="## Customer Experience Test - Alienware (Linux/WSL)

**Date**: $(date -u '+%Y-%m-%d %H:%M UTC')
**Machine**: $(hostname) | **GPU**: $GPU
**Verdict**: $(if [[ $FAILURES -eq 0 ]]; then echo 'ALL PASSED'; else echo "$FAILURES ISSUE(S)"; fi)

---
*Customer Experience Test - \`run-alienware.sh\`*"

    echo "$BODY" | gh issue comment $GITHUB_ISSUE --body-file -
    echo "  Posted to GitHub issue #$GITHUB_ISSUE"
fi

echo ""
exit $FAILURES
