#!/usr/bin/env bash
set -e

PUBLISHER="Lensetek International, LLC."
PUBLISHER_URL="https://lensetek.com"
REPO_NAME="BrevetAB-Agent-Skills"
REPO_URL="https://huggingface.co/spaces/lensetek/brevetab-agent-skills.git"

echo "===================================================================="
echo "     $REPO_NAME - UNIVERSAL INSTALLER WIZARD"
echo "     Publisher: $PUBLISHER ($PUBLISHER_URL)"
echo "===================================================================="
echo "Repository : $REPO_URL"
echo "Scope      : Global AI Agent Installation (Universal)"
echo "Security   : 100% Open-Source Transparent Script"
echo "--------------------------------------------------------------------"
echo ""

# Step 1: Check Node.js & npm
echo "[1/3] Checking Node.js runtime environment..."
if command -v node >/dev/null 2>&1; then
    NODE_VER=$(node -v)
    echo "      [OK] Node.js is detected: $NODE_VER"
else
    echo "      [WARN] Node.js runtime was not found."
    echo "      Please install Node.js LTS (e.g. via brew install node, or nvm, or apt)."
    echo "      Visit: https://nodejs.org/"
    exit 1
fi
echo ""

# Step 2: Global Registration via NPX
echo "[2/3] Installing All 16 Brevet AB Tax Agent Skills GLOBALLY..."
echo "      Executing: npx --yes skills add -g $REPO_URL -y"
echo ""

npx --yes skills add -g "$REPO_URL" -y

echo ""
echo "===================================================================="
echo "[SUCCESS] Brevet AB Tax Agent Skills installed GLOBALLY!"
echo "All AI Agents now recognize these 16 skills across all your workspaces!"
echo "===================================================================="
echo ""

# Step 3: Optional AI Desktop Client Download
echo "[3/3] RECOMMENDED: AI DESKTOP CLIENTS (OPTIONAL)"
echo "Enhances your agent experience with modern graphical desktop workspaces:"
echo ""
echo "  [1] Freebuff Desktop (Universal AI Agent Workspace)"
echo "      URL: https://freebuff.com/api/desktop/download/windows"
echo ""
echo "  [2] OpenCode Desktop (macOS / Linux)"
echo "      URL: https://opencode.ai/download"
echo ""
echo "  [3] Skip and Finish"
echo ""

read -p "Select an option [1-3] (Default: 3): " CLIENT_CHOICE
CLIENT_CHOICE=${CLIENT_CHOICE:-3}

open_url() {
    local url="$1"
    if command -v xdg-open >/dev/null 2>&1; then
        xdg-open "$url"
    elif command -v open >/dev/null 2>&1; then
        open "$url"
    else
        echo "Please visit: $url"
    fi
}

if [ "$CLIENT_CHOICE" = "1" ]; then
    echo "Opening Freebuff Desktop download..."
    open_url "https://freebuff.com/api/desktop/download/windows"
elif [ "$CLIENT_CHOICE" = "2" ]; then
    echo "Opening OpenCode Desktop download..."
    open_url "https://opencode.ai/download"
else
    echo "Skipping desktop client download."
fi

echo ""
echo "===================================================================="
echo "     SETUP COMPLETE! Powered by $PUBLISHER"
echo "===================================================================="
