#!/bin/bash
# ClaudeAll - Universal Installer
# Works on: Linux, macOS, WSL, Termux
# Usage: curl -fsSL https://raw.githubusercontent.com/zesbe/ClaudeAll/main/install-universal.sh | bash

set -e

echo "╔════════════════════════════════════════════════════════════╗"
echo "║  🤖 ClaudeAll - Universal Installer                        ║"
echo "║  MONSTER ENGINEER + Raja Terakhir Mode                     ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""

# Detect platform
PLATFORM="unknown"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    if [ -d /data/data/com.termux ]; then
        PLATFORM="termux"
    else
        PLATFORM="linux"
    fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
    PLATFORM="macos"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
    PLATFORM="windows"
fi

echo "📍 Platform: $PLATFORM"
echo ""

# Check Node.js
if ! command -v node &> /dev/null; then
    echo "❌ Node.js not found!"
    echo ""
    echo "Install Node.js first:"
    case $PLATFORM in
        linux)
            echo "  curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -"
            echo "  sudo apt-get install -y nodejs"
            ;;
        macos)
            echo "  brew install node"
            ;;
        termux)
            echo "  pkg install nodejs"
            ;;
        *)
            echo "  Visit: https://nodejs.org/"
            ;;
    esac
    exit 1
fi

echo "✅ Node.js $(node --version)"
echo "✅ npm $(npm --version)"
echo ""

# Install Claude CLI
if ! command -v claude &> /dev/null; then
    echo "📥 Installing Claude CLI (native installer)..."
    case $PLATFORM in
        linux|macos)
            # Native installer (recommended) - puts binary in ~/.local/bin/
            if curl -fsSL https://claude.ai/install.sh | bash; then
                echo "✅ Claude CLI installed via native installer"
                # Ensure ~/.local/bin is in PATH for current session
                export PATH="$HOME/.local/bin:$PATH"
            else
                echo "⚠️  Native installer failed, falling back to npm..."
                npm install -g @anthropic-ai/claude-code
            fi
            ;;
        termux)
            # Termux/Android: native binary not supported, use npm
            echo "ℹ️  Termux detected, using npm (native installer not supported)"
            npm install -g @anthropic-ai/claude-code
            ;;
        windows)
            # Windows under msys/cygwin - prefer native PowerShell installer if possible
            echo "ℹ️  For Windows native install, run in PowerShell:"
            echo "    irm https://claude.ai/install.ps1 | iex"
            echo "   Falling back to npm for this session..."
            npm install -g @anthropic-ai/claude-code
            ;;
        *)
            npm install -g @anthropic-ai/claude-code
            ;;
    esac
else
    echo "✅ Claude CLI already installed ($(command -v claude))"
fi

# Install ClaudeAll config
echo ""
echo "📦 Installing ClaudeAll configuration..."
npm install -g claude-all-config@latest

echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║  ✅ ClaudeAll Installation Complete!                       ║"
echo "╠════════════════════════════════════════════════════════════╣"
echo "║                                                            ║"
echo "║  🤖 MONSTER ENGINEER Mode Activated                        ║"
echo "║  👑 Raja Terakhir Authority Enabled                        ║"
echo "║                                                            ║"
echo "║  Installed:                                                ║"
echo "║  • @proactive-mode agent (auto-enabled)                    ║"
echo "║  • 14 AI Agents                                            ║"
echo "║  • 63 Skills                                               ║"
echo "║  • 7 MCP Servers                                           ║"
echo "║                                                            ║"
echo "║  Run: claude                                               ║"
echo "║                                                            ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""
