@echo off
echo.
echo 📦 Gooner Training Academy - Distribution Packager
echo =================================================
echo.

:: Set distribution info
set DIST_NAME=Gooner-Training-Academy-v4.1-Beta
set BUILD_DATE=%DATE:~-4,4%-%DATE:~-10,2%-%DATE:~-7,2%
set OUTPUT_DIR=..\%DIST_NAME%

echo 🎯 Creating distribution package: %DIST_NAME%
echo 📅 Build Date: %BUILD_DATE%
echo 📁 Output Directory: %OUTPUT_DIR%
echo.

:: Clean previous build
if exist "%OUTPUT_DIR%" (
    echo 🧹 Cleaning previous build...
    rmdir /s /q "%OUTPUT_DIR%"
)

:: Create distribution directory
echo 📂 Creating distribution directory...
mkdir "%OUTPUT_DIR%"

:: Copy core files
echo 📄 Copying core application files...
copy index.html "%OUTPUT_DIR%\"
copy quick-play.html "%OUTPUT_DIR%\"
copy training-academy.html "%OUTPUT_DIR%\"
copy porn-cinema.html "%OUTPUT_DIR%\"
copy player-stats.html "%OUTPUT_DIR%\"
copy user-profile.html "%OUTPUT_DIR%\"
copy hypno-gallery.html "%OUTPUT_DIR%\"
copy package.json "%OUTPUT_DIR%\"

:: Copy documentation
echo 📚 Copying documentation...
copy README.md "%OUTPUT_DIR%\"
@REM copy TESTER_GUIDE.md "%OUTPUT_DIR%\"
@REM copy INSTALLATION_GUIDE.md "%OUTPUT_DIR%\"
@REM copy README-DESKTOP.md "%OUTPUT_DIR%\"

:: Copy setup scripts
echo 🔧 Copying setup scripts...
copy scripts\setup.bat "%OUTPUT_DIR%\"
copy scripts\setup.sh "%OUTPUT_DIR%\"
copy scripts\Start-webgame.bat "%OUTPUT_DIR%\"

:: Copy source code (excluding user data)
echo 💻 Copying source code...
xcopy src "%OUTPUT_DIR%\src\" /e /i /q

:: Copy assets (clean)
echo 🎨 Copying clean assets...
xcopy assets "%OUTPUT_DIR%\assets\" /e /i /q

:: Copy empty directories for user content
echo 📁 Creating user content directories...
mkdir "%OUTPUT_DIR%\photos"
mkdir "%OUTPUT_DIR%\photos\captured"
mkdir "%OUTPUT_DIR%\audio"
mkdir "%OUTPUT_DIR%\videos"
mkdir "%OUTPUT_DIR%\videos\recorded"
mkdir "%OUTPUT_DIR%\backups"

:: Copy sample audio (if exists)
if exist audio\*.mp3 (
    echo 🎵 Copying sample audio files...
    xcopy audio "%OUTPUT_DIR%\audio\" /e /i /q /y
)


:: Create distribution info file
echo 📋 Creating distribution info...
echo # Gooner Training Academy - Distribution Info > "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo Version: 4.1 Beta >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo Build Date: %BUILD_DATE% >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo Package Type: Beta Testing Build >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo. >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo INSTALLATION: >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo 1. Run setup.bat (Windows) or setup.sh (Mac/Linux) >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo 2. Follow setup prompts >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo 3. Launch with Start-webgame.bat or npm start >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo. >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo DOCUMENTATION: >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - TESTER_GUIDE.md: Testing instructions >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - INSTALLATION_GUIDE.md: Detailed setup help >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - README.md: Complete documentation >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo. >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo REQUIREMENTS: >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - Node.js 16+ >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - Modern browser (Chrome 90+, Firefox 88+) >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - Webcam for full functionality >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"
echo - 100MB+ free storage >> "%OUTPUT_DIR%\DISTRIBUTION_INFO.txt"

:: Set executable permissions for shell scripts
echo 🔐 Setting permissions...
if exist "%OUTPUT_DIR%\setup.sh" (
    icacls "%OUTPUT_DIR%\setup.sh" /grant Everyone:F >nul 2>&1
)

:: Clean up any development files that may have been copied
echo 🧹 Final cleanup...
if exist "%OUTPUT_DIR%\.git" rmdir /s /q "%OUTPUT_DIR%\.git"
if exist "%OUTPUT_DIR%\node_modules" rmdir /s /q "%OUTPUT_DIR%\node_modules"
if exist "%OUTPUT_DIR%\.DS_Store" del /q "%OUTPUT_DIR%\.DS_Store"
if exist "%OUTPUT_DIR%\package-lock.json" del /q "%OUTPUT_DIR%\package-lock.json"

:: File count summary
echo.
echo 📊 Distribution Summary:
echo =====================
for /f %%i in ('dir "%OUTPUT_DIR%" /s /b /a-d ^| find /c /v ""') do set FILE_COUNT=%%i
echo Total Files: %FILE_COUNT%

:: Calculate directory size (approximate)
for /f "tokens=3" %%a in ('dir "%OUTPUT_DIR%" /s /-c ^| find "bytes"') do set DIR_SIZE=%%a
echo Directory Size: %DIR_SIZE% bytes

echo.
echo ✅ Distribution package created successfully!
echo 📍 Location: %OUTPUT_DIR%
echo.
echo 🎯 Next Steps:
echo 1. Test the distribution package on a clean system
echo 2. Verify all features work correctly
echo 3. Create archive for distribution (ZIP/7z)
echo 4. Share with testers
echo.

set /p CREATE_ZIP="📦 Create ZIP archive for distribution? (y/n): "
if /i "%CREATE_ZIP%"=="y" (
    echo.
    echo 🗜️ Creating ZIP archive...
    
    :: Use PowerShell to create ZIP (Windows 8+)
    powershell -command "Compress-Archive -Path '%OUTPUT_DIR%\*' -DestinationPath '%DIST_NAME%.zip' -Force"
    
    if exist "%DIST_NAME%.zip" (
        echo ✅ ZIP archive created: %DIST_NAME%.zip
        for %%i in ("%DIST_NAME%.zip") do echo 📏 Archive Size: %%~zi bytes
    ) else (
        echo ❌ Failed to create ZIP archive
        echo Try creating manually or use 7-Zip/WinRAR
    )
)

echo.
echo 🎉 Distribution packaging complete!
pause