#!/usr/bin/env bash
# TopForge — One-click installer for Mac + Linux. Works from any folder.
# Fetches the wheel directly from the empire server, no `pip install .` weirdness.
set -e

WHEEL_URL="https://iamsuperio.cloud/downloads/topforge/topforge-0.1.0-py3-none-any.whl"

cat <<'BANNER'
===============================================================
     TOPFORGE — Emaaa Empire AI Coding Agent
     One-click installer for Mac + Linux
===============================================================
BANNER

# 1. Python check
if ! command -v python3 >/dev/null 2>&1; then
    cat <<EOF
Python 3 is not installed. Install it first:
  Mac:   brew install python3
         (or download from https://www.python.org/downloads/ )
  Linux (Debian/Ubuntu): sudo apt install python3 python3-pip
  Linux (RHEL/Alma):     sudo dnf install python3 python3-pip

Then re-run this installer.
EOF
    exit 1
fi

# 2. pip check
if ! python3 -m pip --version >/dev/null 2>&1; then
    echo "Bootstrapping pip..."
    python3 -m ensurepip --upgrade || {
        echo "Could not install pip. Please install it manually."
        exit 1
    }
fi

echo "[1/3] Upgrading pip + requests..."
python3 -m pip install --user --quiet --upgrade pip requests

echo "[2/3] Installing TopForge from empire server..."
python3 -m pip install --user --force-reinstall --upgrade "$WHEEL_URL"

# 3. Find user-site bin dir and advise PATH
USERBASE=$(python3 -c 'import site; print(site.USER_BASE)')
USERBIN="$USERBASE/bin"

echo "[3/3] Verifying install..."
if [ -x "$USERBIN/topforge" ]; then
    INSTALL_PATH="$USERBIN/topforge"
    echo "    ✓ Installed to: $INSTALL_PATH"
else
    # Some Python setups put scripts elsewhere
    INSTALL_PATH=$(python3 -c 'import subprocess,sys; r=subprocess.run([sys.executable,"-m","pip","show","-f","topforge"],capture_output=True,text=True); [print(l.strip()) for l in r.stdout.split("\n") if "bin/topforge" in l or "Scripts/topforge" in l]' | head -1)
    echo "    ✓ Installed (path may vary: $INSTALL_PATH)"
fi

# 4. PATH guidance
case "$SHELL" in
    */zsh)  RC="$HOME/.zshrc" ;;
    */bash) RC="$HOME/.bashrc" ;;
    *)      RC="$HOME/.profile" ;;
esac

if ! echo "$PATH" | grep -q "$USERBIN"; then
    echo ""
    echo "    Adding $USERBIN to PATH in $RC..."
    echo "" >> "$RC"
    echo "# TopForge installer added this line" >> "$RC"
    echo "export PATH=\"$USERBIN:\$PATH\"" >> "$RC"
    export PATH="$USERBIN:$PATH"
    echo "    ✓ PATH updated. Restart your terminal OR run: source $RC"
fi

# 5. Try the installed command
echo ""
echo "==============================================================="
if command -v topforge >/dev/null 2>&1; then
    topforge --version
    echo ""
    echo "    ✓ TOPFORGE INSTALLED SUCCESSFULLY"
    echo ""
    echo "    To use it, open a new terminal and type:"
    echo "        topforge -p \"say hello\""
    echo ""
    echo "    Interactive mode:"
    echo "        topforge"
else
    echo "    Install finished but 'topforge' isn't on PATH in this shell."
    echo "    Open a NEW terminal and try: topforge --version"
    echo "    Or run directly: $USERBIN/topforge --version"
fi
echo "==============================================================="
