#!/bin/bash

# Colors for 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 - Distribution Packager${NC}"
echo -e "${PURPLE}==================================================${NC}"
echo ""

# Set distribution info
DIST_NAME="Gooner-Training-Academy-v4.0-Beta"
BUILD_DATE=$(date +%Y-%m-%d)
OUTPUT_DIR="../${DIST_NAME}"

echo -e "${CYAN}🎯 Creating distribution package: ${DIST_NAME}${NC}"
echo -e "${CYAN}📅 Build Date: ${BUILD_DATE}${NC}"
echo -e "${CYAN}📁 Output Directory: ${OUTPUT_DIR}${NC}"
echo ""

# Clean previous build
if [ -d "$OUTPUT_DIR" ]; then
    echo -e "${YELLOW}🧹 Cleaning previous build...${NC}"
    rm -rf "$OUTPUT_DIR"
fi

# Create distribution directory
echo -e "${BLUE}📂 Creating distribution directory...${NC}"
mkdir -p "$OUTPUT_DIR"

# Copy core files
echo -e "${BLUE}📄 Copying core application files...${NC}"
cp index.html quick-play.html training-academy.html porn-cinema.html "$OUTPUT_DIR/"
cp player-stats.html user-profile.html package.json "$OUTPUT_DIR/"

# Copy documentation
echo -e "${BLUE}📚 Copying documentation...${NC}"
cp README.md TESTER_GUIDE.md INSTALLATION_GUIDE.md "$OUTPUT_DIR/"
if [ -f README-DESKTOP.md ]; then
    cp README-DESKTOP.md "$OUTPUT_DIR/"
fi

# Copy setup scripts
echo -e "${BLUE}🔧 Copying setup scripts...${NC}"
cp scripts/setup.bat scripts/setup.sh "$OUTPUT_DIR/"
if [ -f scripts/Start-webgame.bat ]; then
    cp scripts/Start-webgame.bat "$OUTPUT_DIR/"
fi

# Copy source code (excluding user data)
echo -e "${BLUE}💻 Copying source code...${NC}"
cp -r src "$OUTPUT_DIR/"

# Copy assets (clean)
echo -e "${BLUE}🎨 Copying clean assets...${NC}"
if [ -d assets ]; then
    cp -r assets "$OUTPUT_DIR/"
fi

# Copy empty directories for user content
echo -e "${BLUE}📁 Creating user content directories...${NC}"
mkdir -p "$OUTPUT_DIR/images/tasks"
mkdir -p "$OUTPUT_DIR/images/consequences"
mkdir -p "$OUTPUT_DIR/audio"
mkdir -p "$OUTPUT_DIR/videos"
mkdir -p "$OUTPUT_DIR/backups"

# Copy sample audio (if exists)
if [ -d audio ] && [ "$(ls -A audio 2>/dev/null)" ]; then
    echo -e "${BLUE}🎵 Copying sample audio files...${NC}"
    cp -r audio/* "$OUTPUT_DIR/audio/" 2>/dev/null || true
fi

# Create placeholder files
echo -e "${BLUE}📝 Creating placeholder files...${NC}"

cat > "$OUTPUT_DIR/images/tasks/README.md" << EOF
# User Task Images

Upload your custom task images here. Supported formats: JPG, PNG, WebP
Maximum 50 images, recommended resolution: 1600x1200

These images are displayed during task scenarios and training sessions.
EOF

cat > "$OUTPUT_DIR/images/consequences/README.md" << EOF
# User Consequence Images

Upload your custom consequence images here. Supported formats: JPG, PNG, WebP
These images are used for punishment scenarios when tasks are skipped or failed.

Organize your content to enhance the training experience.
EOF

cat > "$OUTPUT_DIR/audio/README.md" << EOF
# Background Music

Place your background music files here. Supported formats: MP3, WAV, OGG
Files will be automatically detected and added to the playlist.

Music enhances the immersive training experience across all game modes.
EOF

cat > "$OUTPUT_DIR/videos/README.md" << EOF
# Video Directory

Link external video directories here or place video files directly.
Supported formats: MP4, WebM, AVI, MOV

For best performance, use MP4 format with H.264 codec.
Videos can be used as background content during training sessions.
EOF

# Create distribution info file
echo -e "${BLUE}📋 Creating distribution info...${NC}"
cat > "$OUTPUT_DIR/DISTRIBUTION_INFO.txt" << EOF
# Gooner Training Academy - Distribution Info

Version: 4.0 Beta
Build Date: ${BUILD_DATE}
Package Type: Beta Testing Build

INSTALLATION:
1. Run setup.sh (Mac/Linux) or setup.bat (Windows)
2. Follow setup prompts
3. Launch with 'npm start'

DOCUMENTATION:
- TESTER_GUIDE.md: Testing instructions
- INSTALLATION_GUIDE.md: Detailed setup help
- README.md: Complete documentation

REQUIREMENTS:
- Node.js 16+
- Modern browser (Chrome 90+, Firefox 88+)
- Webcam for full functionality
- 100MB+ free storage

TESTING FOCUS:
- Core game modes functionality
- Webcam integration stability
- Performance during extended sessions
- Cross-platform compatibility
- Media library management

SUPPORT:
Report issues with detailed reproduction steps and system information.
Include console logs when possible (F12 in browser).
EOF

# Set executable permissions
echo -e "${BLUE}🔐 Setting permissions...${NC}"
chmod +x "$OUTPUT_DIR/setup.sh"

# Clean up any development files that may have been copied
echo -e "${YELLOW}🧹 Final cleanup...${NC}"
rm -rf "$OUTPUT_DIR/.git" 2>/dev/null || true
rm -rf "$OUTPUT_DIR/node_modules" 2>/dev/null || true
rm -f "$OUTPUT_DIR/.DS_Store" 2>/dev/null || true
rm -f "$OUTPUT_DIR/package-lock.json" 2>/dev/null || true
rm -f "$OUTPUT_DIR/.gitignore" 2>/dev/null || true

# File count summary
echo ""
echo -e "${GREEN}📊 Distribution Summary:${NC}"
echo -e "${GREEN}=====================${NC}"
FILE_COUNT=$(find "$OUTPUT_DIR" -type f | wc -l)
echo -e "Total Files: ${FILE_COUNT}"

# Calculate directory size
DIR_SIZE=$(du -sh "$OUTPUT_DIR" | cut -f1)
echo -e "Directory Size: ${DIR_SIZE}"

echo ""
echo -e "${GREEN}✅ Distribution package created successfully!${NC}"
echo -e "${GREEN}📍 Location: ${OUTPUT_DIR}${NC}"
echo ""
echo -e "${CYAN}🎯 Next Steps:${NC}"
echo "1. Test the distribution package on a clean system"
echo "2. Verify all features work correctly"  
echo "3. Create archive for distribution (tar.gz/zip)"
echo "4. Share with testers"
echo ""

read -p "📦 Create TAR.GZ archive for distribution? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo ""
    echo -e "${BLUE}🗜️ Creating TAR.GZ archive...${NC}"
    
    cd ..
    tar -czf "${DIST_NAME}.tar.gz" "${DIST_NAME}/"
    cd - > /dev/null
    
    if [ -f "../${DIST_NAME}.tar.gz" ]; then
        echo -e "${GREEN}✅ TAR.GZ archive created: ${DIST_NAME}.tar.gz${NC}"
        ARCHIVE_SIZE=$(du -sh "../${DIST_NAME}.tar.gz" | cut -f1)
        echo -e "${GREEN}📏 Archive Size: ${ARCHIVE_SIZE}${NC}"
    else
        echo -e "${RED}❌ Failed to create TAR.GZ archive${NC}"
        echo "Try creating manually with: tar -czf ${DIST_NAME}.tar.gz ${DIST_NAME}/"
    fi
fi

echo ""
echo -e "${GREEN}🎉 Distribution packaging complete!${NC}"
echo ""