#!/bin/bash

# StreamFlow - Platform Asset Generator
# Generates optimized assets for PWA, Android, and Desktop

set -e

echo "🎨 StreamFlow Platform Asset Generator"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

cd "$(dirname "$0")/.."
PROJECT_ROOT="$(pwd)"

# Check for ImageMagick
if ! command -v convert &> /dev/null; then
    echo "❌ ImageMagick is not installed. Installing..."
    if command -v apt-get &> /dev/null; then
        sudo apt-get update && sudo apt-get install -y imagemagick
    elif command -v brew &> /dev/null; then
        brew install imagemagick
    else
        echo "Please install ImageMagick manually: https://imagemagick.org/script/download.php"
        exit 1
    fi
fi

SOURCE_ICON="frontend/public/icons/icon-512x512.svg"

if [ ! -f "$SOURCE_ICON" ]; then
    echo "❌ Source icon not found: $SOURCE_ICON"
    exit 1
fi

echo ""
echo "📱 Generating PWA icons..."
PWA_SIZES=(72 96 128 144 152 192 384 512)
for size in "${PWA_SIZES[@]}"; do
    echo "   → ${size}x${size}px"
    convert "$SOURCE_ICON" -resize ${size}x${size} "frontend/public/icons/icon-${size}x${size}.png"
done

echo ""
echo "🍎 Generating iOS icons..."
convert "$SOURCE_ICON" -resize 180x180 "frontend/public/icons/apple-touch-icon.png"
convert "$SOURCE_ICON" -resize 167x167 "frontend/public/icons/apple-touch-icon-ipad.png"

echo ""
echo "🤖 Generating Android icons..."
ANDROID_SIZES=(48 72 96 144 192)
ANDROID_DENSITIES=(mdpi hdpi xhdpi xxhdpi xxxhdpi)
mkdir -p frontend/public/android-icons

for i in "${!ANDROID_SIZES[@]}"; do
    size="${ANDROID_SIZES[$i]}"
    density="${ANDROID_DENSITIES[$i]}"
    echo "   → ${density} (${size}x${size}px)"
    convert "$SOURCE_ICON" -resize ${size}x${size} "frontend/public/android-icons/icon-${density}.png"
done

echo ""
echo "🐧 Generating Linux Desktop icons..."
convert "$SOURCE_ICON" -resize 512x512 "desktop-app/build/icon.png"
cp "$SOURCE_ICON" "desktop-app/build/icon.svg"

echo ""
echo "🌐 Generating favicon..."
convert "$SOURCE_ICON" -resize 32x32 "frontend/public/favicon.png"
convert "$SOURCE_ICON" -resize 16x16 -resize 32x32 -resize 48x48 "frontend/public/favicon.ico"

echo ""
echo "📄 Generating splash screens..."
# PWA splash screens for iOS
IOS_SPLASH_SIZES=(
    "640x1136:iphone5"
    "750x1334:iphone6"
    "828x1792:iphone11"
    "1125x2436:iphonex"
    "1242x2208:iphone6plus"
    "1242x2688:iphonemax"
    "1536x2048:ipad"
    "1668x2224:ipadpro10"
    "1668x2388:ipadpro11"
    "2048x2732:ipadpro12"
)

mkdir -p frontend/public/splash

for splash_config in "${IOS_SPLASH_SIZES[@]}"; do
    IFS=':' read -r size name <<< "$splash_config"
    IFS='x' read -r width height <<< "$size"
    echo "   → ${name} (${width}x${height}px)"
    
    # Create splash with icon centered on background
    convert -size ${width}x${height} xc:"#121212" \
        \( "$SOURCE_ICON" -resize 256x256 \) \
        -gravity center -composite \
        "frontend/public/splash/splash-${name}.png"
done

echo ""
echo "✅ Asset generation complete!"
echo ""
echo "📦 Generated assets:"
echo "   PWA: frontend/public/icons/*.png"
echo "   iOS: frontend/public/icons/apple-touch-icon*.png"
echo "   Android: frontend/public/android-icons/*.png"
echo "   Desktop: desktop-app/build/icon.{png,svg}"
echo "   Favicon: frontend/public/favicon.{ico,png}"
echo "   Splash: frontend/public/splash/*.png"
echo ""
echo "🔄 Next: Rebuild Docker container to include new assets"
echo "   docker compose build --no-cache"
