#!/usr/bin/env bash
set -euo pipefail

# Atlas Memory — curl-installable bootstrap
# Usage: curl -sSL https://raw.githubusercontent.com/.../install.sh | bash

BOLD='\033[1m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

info() { echo -e "${GREEN}[atlas-memory]${NC} $1"; }
warn() { echo -e "${YELLOW}[atlas-memory]${NC} $1"; }

# Check for Python 3.9+
check_python() {
    if command -v python3 &>/dev/null; then
        PYTHON=python3
    elif command -v python &>/dev/null; then
        PYTHON=python
    else
        echo "Error: Python 3.9+ is required but not found."
        echo "Install Python from https://python.org or via your package manager."
        exit 1
    fi

    version=$($PYTHON -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
    major=$(echo "$version" | cut -d. -f1)
    minor=$(echo "$version" | cut -d. -f2)

    if [ "$major" -lt 3 ] || { [ "$major" -eq 3 ] && [ "$minor" -lt 9 ]; }; then
        echo "Error: Python 3.9+ required, found $version"
        exit 1
    fi

    info "Found Python $version"
}

# Install atlas-memory
install() {
    # Prefer uv if available (much faster)
    if command -v uv &>/dev/null; then
        info "Installing with uv..."
        uv pip install atlas-memory
    elif command -v uvx &>/dev/null; then
        info "Running with uvx..."
        uvx atlasmemory init
        return
    elif command -v pipx &>/dev/null; then
        info "Installing with pipx..."
        pipx install atlas-memory
    else
        info "Installing with pip..."
        $PYTHON -m pip install --user atlas-memory
    fi
}

main() {
    echo -e "${BOLD}Atlas Memory — Persistent Knowledge for AI Agents${NC}"
    echo ""

    check_python
    install

    echo ""
    info "Installation complete!"
    echo ""
    echo "  Get started:"
    echo "    cd your-project"
    echo "    atlasmemory init"
    echo ""
}

main "$@"
