#!/bin/bash
# AgntSpace installer
# Usage: curl -fsSL https://www.agntspace.com/install.sh | bash
set -e

echo ""
echo "  AGNTSPACE — Your AI partner"
echo "  ============================="
echo ""

# Find Python 3.11+
PY=""
for cmd in python3 python; do
    if command -v "$cmd" &> /dev/null; then
        ver=$("$cmd" -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" 2>/dev/null || echo "0.0")
        major=$(echo "$ver" | cut -d. -f1)
        minor=$(echo "$ver" | cut -d. -f2)
        if [ "$major" -ge 3 ] && [ "$minor" -ge 11 ]; then
            PY="$cmd"
            break
        fi
    fi
done

if [ -z "$PY" ]; then
    echo "  Python 3.11+ is required."
    echo "  Download from: https://www.python.org/downloads/"
    echo ""
    exit 1
fi

echo "  $($PY --version) found"

# Ensure pip is available
$PY -m ensurepip --upgrade 2>/dev/null || true

# Install
echo "  Installing agntspace..."
if ! $PY -m pip install --upgrade --no-cache-dir agntspace 2>/dev/null; then
    echo ""
    echo "  pip install failed. The package may not be published yet."
    echo ""
    echo "  Install from source instead:"
    echo "    git clone https://github.com/agntspace/agntspace.git"
    echo "    cd agntspace/server"
    echo "    pip install -e ."
    echo "    agntspace init"
    echo "    agntspace start"
    echo ""
    exit 1
fi

# Verify
if ! command -v agntspace &> /dev/null; then
    echo "  agntspace command not found. Try: $PY -m agntspace.cli --help"
    exit 1
fi

# Initialize
echo "  Initializing..."
agntspace init --minimal

echo ""
echo "  Done! Start your agent:"
echo ""
echo "    agntspace start"
echo ""
echo "  Then open http://localhost:8000"
echo ""
