#!/bin/bash

set -e

OUTPUT_DIR=${OUTPUT_DIR:-/output}
FILE_SIZES=${FILE_SIZES:-"1KB,10MB,100MB"}
VIDEO_FORMATS=${VIDEO_FORMATS:-"mp4,mkv,avi,mov,webm"}

echo "🗃️  Generating test files..."
echo "Output directory: $OUTPUT_DIR"
echo "File sizes: $FILE_SIZES"
echo "Video formats: $VIDEO_FORMATS"

mkdir -p "$OUTPUT_DIR"
cd "$OUTPUT_DIR"

# Parse file sizes
IFS=',' read -ra SIZES <<< "$FILE_SIZES"
IFS=',' read -ra FORMATS <<< "$VIDEO_FORMATS"

# Helper function to convert size notation to bytes
size_to_bytes() {
    local size="$1"
    local number="${size//[^0-9]/}"
    local unit="${size//[0-9]/}"
    
    case "${unit^^}" in
        "KB") echo $((number * 1024)) ;;
        "MB") echo $((number * 1024 * 1024)) ;;
        "GB") echo $((number * 1024 * 1024 * 1024)) ;;
        *) echo "$number" ;;  # Assume bytes if no unit
    esac
}

# Generate regular test files
for size_spec in "${SIZES[@]}"; do
    size_spec=$(echo "$size_spec" | tr -d ' ')  # Remove spaces
    bytes=$(size_to_bytes "$size_spec")
    filename="test_file_${size_spec}.bin"
    
    echo "Creating $filename ($bytes bytes)..."
    head -c "$bytes" /dev/urandom > "$filename"
done

# Generate video files for each format
for format in "${FORMATS[@]}"; do
    format=$(echo "$format" | tr -d ' ')  # Remove spaces
    
    # Create different sizes for video files
    for size_spec in "1MB" "5MB" "10MB"; do
        bytes=$(size_to_bytes "$size_spec")
        filename="test_video_${size_spec}.${format}"
        
        echo "Creating $filename ($bytes bytes)..."
        head -c "$bytes" /dev/urandom > "$filename"
    done
done

# Create special test files
echo "Creating special test files..."

# Empty file
touch "empty_file.txt"

# Text file with known content
cat << 'EOF' > "text_file.txt"
This is a test text file for the Blossom-BitTorrent Gateway.
It contains multiple lines of text to test text file handling.
This file can be used to verify text processing capabilities.
The content is predictable and can be verified after upload/download.

Line numbers:
1. First line
2. Second line
3. Third line
4. Fourth line
5. Fifth line

Special characters: !@#$%^&*()_+-=[]{}|;':\",./<>?
Unicode: 🚀 🌟 💫 ⚡ 🔥 ⭐ 🎯 🎪 🎨 🎭

End of test file.
EOF

# Binary file with pattern
echo "Creating binary pattern file..."
python3 -c "
import struct
with open('binary_pattern.bin', 'wb') as f:
    for i in range(1024):
        f.write(struct.pack('I', i))
" 2>/dev/null || {
    # Fallback if python3 is not available
    for i in $(seq 0 1023); do
        printf "\\$(printf "%03o" $((i % 256)))" >> binary_pattern.bin
    done
}

# Create JSON metadata file
cat << EOF > "test_files_manifest.json"
{
  "generated_at": "$(date -Iseconds)",
  "files": [
EOF

first_file=true
for file in *.bin *.txt *.mp4 *.mkv *.avi *.mov *.webm; do
    if [[ -f "$file" ]]; then
        if [[ "$first_file" != true ]]; then
            echo "    }," >> "test_files_manifest.json"
        fi
        first_file=false
        
        size=$(wc -c < "$file")
        sha256sum_value=$(sha256sum "$file" | cut -d' ' -f1)
        
        cat << EOF >> "test_files_manifest.json"
    {
      "filename": "$file",
      "size": $size,
      "sha256": "$sha256sum_value"
EOF
    fi
done

if [[ "$first_file" != true ]]; then
    echo "    }" >> "test_files_manifest.json"
fi

cat << 'EOF' >> "test_files_manifest.json"
  ]
}
EOF

echo "📋 Test files generated successfully:"
ls -lah

echo ""
echo "📊 Summary:"
echo "Total files: $(find . -type f | wc -l)"
echo "Total size: $(du -sh . | cut -f1)"

echo ""
echo "✅ Test file generation complete!"