@echo off
echo.
echo 🎮 Gooner Training Academy - Beta Testing Setup
echo ================================================
echo Version: 4.0 Beta ^| Build: November 2025
echo.

:: Create a colorful header
echo ⚠️  ADULT CONTENT - 18+ ONLY ⚠️
echo.

:: Check if running as administrator (recommended)
net session >nul 2>&1
if errorlevel 1 (
    echo ℹ️  Note: Running without administrator privileges
    echo    Some features may be limited
    echo.
)

:: System information
echo 📊 System Check:
echo ---------------

:: Check if Node.js is installed
echo Checking Node.js...
node --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Node.js is NOT installed
    echo.
    echo 📥 Please install Node.js first:
    echo    1. Visit: https://nodejs.org/en/download/
    echo    2. Download LTS version (v18+ recommended)
    echo    3. Run installer with default settings
    echo    4. Restart this setup after installation
    echo.
    pause
    exit /b 1
) else (
    for /f "tokens=*" %%i in ('node --version') do set NODE_VERSION=%%i
    echo ✅ Node.js !NODE_VERSION! detected
)

:: Check if npm is installed
echo Checking npm...
npm --version >nul 2>&1
if errorlevel 1 (
    echo ❌ npm is NOT installed
    echo    npm usually comes with Node.js
    echo    Please reinstall Node.js from nodejs.org
    echo.
    pause
    exit /b 1
) else (
    for /f "tokens=*" %%i in ('npm --version') do set NPM_VERSION=%%i
    echo ✅ npm v!NPM_VERSION! detected
)

:: Check available disk space (approximate)
echo Checking disk space...
for /f "tokens=3" %%a in ('dir /-c ^| find "bytes free"') do set FREE_SPACE=%%a
echo ✅ Sufficient disk space available

echo.
echo 📦 Installing Dependencies:
echo --------------------------

:: Create backup of package-lock.json if it exists
if exist package-lock.json (
    echo Creating backup of existing installation...
    copy package-lock.json package-lock.json.bak >nul 2>&1
)

:: Install dependencies with progress
echo Installing Electron and build tools...
npm install --silent

if errorlevel 0 (
    echo.
    echo ✅ Installation Complete!
    echo ========================
    echo.
    echo 🚀 Ready to Launch!
    echo.
    echo 🎯 Quick Start Options:
    echo   1. Double-click "scripts/Start-webgame.bat" to launch
    echo   2. Or run "npm start" in this folder
    echo   3. Or open "index.html" in browser (limited features)
    echo.
    echo 📚 Documentation:
    echo   - TESTER_GUIDE.md     : Testing instructions
    echo   - INSTALLATION_GUIDE.md : Detailed setup help
    echo   - README.md           : Full feature documentation
    echo.
    echo 🔧 Advanced Commands:
    echo   npm start           : Launch desktop application
    echo   npm run dev         : Launch with developer tools
    echo   npm run build       : Create executable
    echo   npm run build-win   : Build Windows installer
    echo.
    echo 📞 Need Help?
    echo   - Check TESTER_GUIDE.md for common issues
    echo   - Press F12 in browser for console logs
    echo   - Report bugs with detailed reproduction steps
    echo.
    
    :: Check if we should auto-launch
    set /p AUTOLAUNCH="🚀 Launch application now? (y/n): "
    if /i "%AUTOLAUNCH%"=="y" (
        echo.
        echo Launching Gooner Training Academy...
        start "" npm start
    ) else (
        echo.
        echo 👍 Setup complete! Launch when ready with scripts/Start-webgame.bat
    )
    
) else (
    echo.
    echo ❌ Installation Failed!
    echo =====================
    echo.
    echo 🔍 Troubleshooting Steps:
    echo   1. Check internet connection
    echo   2. Run as Administrator
    echo   3. Clear npm cache: npm cache clean --force
    echo   4. Delete node_modules folder and try again
    echo   5. Restart computer and retry
    echo.
    echo 📞 Still having issues?
    echo   - Check INSTALLATION_GUIDE.md
    echo   - Verify Node.js version is 16+
    echo   - Try manual setup: npm install
    echo.
)

echo.
pause