#!/bin/bash
# Gity CLI Installer - npm-based installation
# Version: 2.0.0 (New NPM Installer)

echo "📦 Installing Gity CLI..."

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is required but not installed."
    echo "📥 Install Node.js from: https://nodejs.org"
    exit 1
fi

# Check if npm is available
if ! command -v npm &> /dev/null; then
    echo "❌ npm is required but not found."
    exit 1
fi

# Install gityai globally
echo "🚀 Running npm install -g gityai..."
npm install -g gityai

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ Gity installed successfully!"
    echo "👉 Run: gity login"
    echo ""
else
    echo "⚠️  Standard installation failed. Trying with sudo..."
    sudo npm install -g gityai
    
    if [ $? -eq 0 ]; then
        echo ""
        echo "✅ Gity installed successfully (with sudo)!"
        echo "👉 Run: gity login"
        echo ""
    else
        echo "❌ Installation failed. Please try running 'sudo npm install -g gityai' manually."
        echo "   Or visit https://gityai.co/docs for help."
        exit 1
    fi
fi
