#!/usr/bin/env bash
# Trading AI Agent Skills - Universal Installer for macOS / Linux
set -e

REPO_URL="https://huggingface.co/spaces/lensetek/trading-ai-agent-skills"

echo "===================================================================="
echo "       TRADING AI AGENT SKILLS - UNIVERSAL INSTALLER WIZARD         "
echo "===================================================================="
echo "Repository : $REPO_URL"
echo "Capacity   : 13 Quantitative & Multi-Market Trading AI Agent Skills"
echo "Scope      : Global Installation (Available across all AI Agents)"
echo "--------------------------------------------------------------------"
echo ""

# 1. Check Node.js and npm
echo "[1/4] Checking Node.js & npm..."
if ! command -v npm >/dev/null 2>&1; then
    echo "      [ERROR] Node.js and npm not found!"
    echo "      The Agent Skills ecosystem and OpenCode CLI require Node.js (v18+)."
    echo "      Please install Node.js from https://nodejs.org"
    exit 1
fi
echo "      [OK] Node.js and npm are available."
echo ""

# 2. Check & Install OpenCode CLI
echo "[2/4] Checking OpenCode CLI setup..."
if command -v opencode >/dev/null 2>&1; then
    echo "      [OK] OpenCode CLI is already installed."
else
    echo "      OpenCode CLI not found. Installing @opencode/cli globally via npm..."
    if command -v npm >/dev/null 2>&1; then
        npm install -g @opencode/cli || sudo npm install -g @opencode/cli
    else
        curl -fsSL https://opencode.ai/v2/install | bash
    fi
fi
echo ""

# 3. Register Skills Globally
echo "[3/4] Installing Trading AI Agent Skills GLOBALLY..."
echo "      Executing: npx -y skills add $REPO_URL -g -y --all"
npx -y skills add "$REPO_URL" -g -y --all
echo ""
echo "===================================================================="
echo "[SUCCESS] 13 Trading AI Agent Skills installed GLOBALLY!"
echo "All AI Agents (OpenCode, Antigravity, Claude Code, Cursor, Codex)"
echo "now recognize these trading skills across all your local workspaces!"
echo "===================================================================="
echo ""

# 4. Optional OpenCode Desktop
echo "[4/4] OPTIONAL: OPENCODE DESKTOP (MODERN GRAPHICAL USER INTERFACE)"
read -p "Would you like to download OpenCode Desktop App? [Y/n]: " choice
choice=${choice:-Y}
if [[ "$choice" =~ ^[Yy]$ ]]; then
    echo "Opening https://opencode.ai/download in your default browser..."
    if command -v xdg-open >/dev/null 2>&1; then
        xdg-open "https://opencode.ai/download"
    elif command -v open >/dev/null 2>&1; then
        open "https://opencode.ai/download"
    else
        echo "Please visit: https://opencode.ai/download"
    fi
fi

echo ""
echo "Setup complete! Enjoy quantitative trading intelligence."
