#!/bin/bash

# Hive.AI Installation Script - Beginner Friendly
# This script installs the Hive.AI MCP tool globally

set -e

echo "🐝 Welcome to Hive.AI Installation!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Check if Node.js is installed
echo "🔍 Checking if Node.js is installed..."
if ! command -v node &> /dev/null; then
    echo ""
    echo "❌ Node.js is not installed on your system."
    echo ""
    echo "📦 To install Node.js:"
    echo "   1. Visit: https://nodejs.org"
    echo "   2. Download the LTS version (recommended)"
    echo "   3. Run the installer"
    echo "   4. Restart your terminal/computer"
    echo "   5. Run this script again"
    echo ""
    exit 1
fi

# Check Node.js version
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 18 ]; then
    echo "⚠️  Your Node.js version is too old (found v$(node -v))"
    echo "   Hive.AI requires Node.js v18 or newer"
    echo "   Please update at: https://nodejs.org"
    exit 1
fi

echo "✅ Node.js $(node -v) found - ready to install!"

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo "❌ npm is not installed. This usually comes with Node.js."
    echo "   Please reinstall Node.js from: https://nodejs.org"
    exit 1
fi

echo "✅ npm $(npm -v) found - ready to go!"
echo ""

# Install the package globally
echo "📦 Installing Hive.AI globally..."
echo "   This may take a minute or two..."
echo "   📝 Note: You may see some deprecation warnings - these are normal"
echo "      and don't affect Hive.AI's functionality!"
echo ""

if npm install -g @hivetechs/hive-ai; then
    echo "✅ Hive.AI installed successfully!"
else
    echo "❌ Installation failed. Trying with different permissions..."
    echo "   You may need to enter your password:"
    sudo npm install -g @hivetechs/hive-ai
fi

echo ""

# Check if installation was successful
if command -v hive-ai &> /dev/null; then
    echo "🎉 Installation complete! Hive.AI is ready to use."
    echo ""
    echo "📚 Next steps:"
    echo "   1. Get a license at: https://hivetechs.io/pricing"
    echo "   2. Configure Hive.AI: hive-ai configure"
    echo "   3. Setup your IDE: hive-ai configure-ide"
    echo "   4. Start coding with AI superpowers! 🚀"
    echo ""
    echo "💡 Need help? Visit: https://hivetechs.io/documentation"
    echo "📧 Support: support@hivetechs.io"
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "🐝 Welcome to the future of AI-assisted coding!"
else
    echo "❌ Installation failed. Please try manual installation:"
    echo "   npm install -g @hivetechs/hive-ai"
    echo ""
    echo "Or contact support: support@hivetechs.io"
    exit 1
fi