#!/bin/bash

# Colors for better output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo ""
echo -e "${PURPLE}🎮 Gooner Training Academy - Beta Testing Setup${NC}"
echo -e "${PURPLE}================================================${NC}"
echo -e "${CYAN}Version: 4.0 Beta | Build: November 2025${NC}"
echo ""

echo -e "${YELLOW}⚠️  ADULT CONTENT - 18+ ONLY ⚠️${NC}"
echo ""

# Detect OS
if [[ "$OSTYPE" == "darwin"* ]]; then
    OS="macOS"
    PACKAGE_MANAGER="brew"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    OS="Linux"
    if command -v apt &> /dev/null; then
        PACKAGE_MANAGER="apt"
    elif command -v yum &> /dev/null; then
        PACKAGE_MANAGER="yum"
    elif command -v pacman &> /dev/null; then
        PACKAGE_MANAGER="pacman"
    else
        PACKAGE_MANAGER="unknown"
    fi
else
    OS="Unknown"
    PACKAGE_MANAGER="unknown"
fi

echo -e "${CYAN}📊 System Check:${NC}"
echo -e "${CYAN}---------------${NC}"
echo -e "Operating System: ${OS}"
echo -e "Package Manager: ${PACKAGE_MANAGER}"

# Check Node.js version
echo ""
echo "Checking Node.js..."
if ! command -v node &> /dev/null; then
    echo -e "${RED}❌ Node.js is NOT installed${NC}"
    echo ""
    echo -e "${YELLOW}📥 Please install Node.js first:${NC}"
    echo "   https://nodejs.org/en/download/"
    echo ""
    echo "Or use your package manager:"
    if [[ "$OS" == "macOS" ]]; then
        echo "   brew install node          # macOS with Homebrew"
    elif [[ "$OS" == "Linux" ]]; then
        echo "   sudo apt install nodejs npm    # Ubuntu/Debian"
        echo "   sudo yum install nodejs npm    # CentOS/RHEL/Fedora"  
        echo "   sudo pacman -S nodejs npm      # Arch Linux"
    fi
    echo ""
    echo "After installation, run this setup script again."
    exit 1
else
    NODE_VERSION=$(node --version)
    echo -e "${GREEN}✅ Node.js ${NODE_VERSION} detected${NC}"
    
    # Check Node.js version (should be 16+)
    NODE_MAJOR=$(echo $NODE_VERSION | cut -d'.' -f1 | sed 's/v//')
    if [ "$NODE_MAJOR" -lt 16 ]; then
        echo -e "${YELLOW}⚠️  Warning: Node.js version is older than recommended (16+)${NC}"
        echo "   Consider updating for best performance"
    fi
fi

# Check npm
echo "Checking npm..."
if ! command -v npm &> /dev/null; then
    echo -e "${RED}❌ npm is NOT installed${NC}"
    echo "   npm usually comes with Node.js"
    echo "   Please reinstall Node.js from nodejs.org"
    exit 1
else
    NPM_VERSION=$(npm --version)
    echo -e "${GREEN}✅ npm v${NPM_VERSION} detected${NC}"
fi

# Check disk space (basic check)
echo "Checking disk space..."
AVAILABLE_SPACE=$(df -h . | awk 'NR==2 {print $4}')
echo -e "${GREEN}✅ Available disk space: ${AVAILABLE_SPACE}${NC}"

echo ""
echo -e "${CYAN}📦 Installing Dependencies:${NC}"
echo -e "${CYAN}--------------------------${NC}"

# Create backup of package-lock.json if it exists
if [ -f "package-lock.json" ]; then
    echo "Creating backup of existing installation..."
    cp package-lock.json package-lock.json.bak
fi

# Install dependencies
echo "Installing Electron and build tools..."
echo "This may take a few minutes..."

npm install --silent

if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✅ Installation Complete!${NC}"
    echo -e "${GREEN}========================${NC}"
    echo ""
    echo -e "${PURPLE}🚀 Ready to Launch!${NC}"
    echo ""
    echo -e "${CYAN}🎯 Quick Start Options:${NC}"
    echo "   1. Run 'npm start' to launch desktop app"
    echo "   2. Open 'index.html' in browser (limited features)"
    echo ""
    echo -e "${CYAN}📚 Documentation:${NC}"
    echo "   - TESTER_GUIDE.md     : Testing instructions"
    echo "   - INSTALLATION_GUIDE.md : Detailed setup help"  
    echo "   - README.md           : Full feature documentation"
    echo ""
    echo -e "${CYAN}🔧 Available Commands:${NC}"
    echo "   npm start           : Launch desktop application"
    echo "   npm run dev         : Launch with developer tools"
    echo "   npm run build       : Create executable"
    if [[ "$OS" == "macOS" ]]; then
        echo "   npm run build-mac   : Build macOS .dmg installer"
    elif [[ "$OS" == "Linux" ]]; then
        echo "   npm run build-linux : Build Linux AppImage"
    fi
    echo ""
    echo -e "${CYAN}📞 Need Help?${NC}"
    echo "   - Check TESTER_GUIDE.md for common issues"
    echo "   - Open browser dev tools (F12) for console logs"
    echo "   - Report bugs with detailed reproduction steps"
    echo ""
    
    # Ask if user wants to auto-launch
    read -p "🚀 Launch application now? (y/n): " -n 1 -r
    echo ""
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo ""
        echo "Launching Gooner Training Academy..."
        npm start &
    else
        echo ""
        echo "👍 Setup complete! Launch when ready with: npm start"
    fi
    
else
    echo ""
    echo -e "${RED}❌ Installation Failed!${NC}"
    echo -e "${RED}=====================${NC}"
    echo ""
    echo -e "${YELLOW}🔍 Troubleshooting Steps:${NC}"
    echo "   1. Check internet connection"
    echo "   2. Clear npm cache: npm cache clean --force"
    echo "   3. Delete node_modules folder and try again"
    echo "   4. Check file permissions in current directory"
    echo "   5. Try running with sudo (if on Linux)"
    echo ""
    echo -e "${YELLOW}📞 Still having issues?${NC}"
    echo "   - Check INSTALLATION_GUIDE.md"
    echo "   - Verify Node.js version is 16+"
    echo "   - Try manual setup: npm install"
    echo "   - Check system requirements"
    echo ""
    exit 1
fi

echo ""