#!/bin/bash

# StreamFlow - Android APK Builder
# Builds Android APK using Capacitor

set -e

echo "📱 StreamFlow Android APK Builder"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Check prerequisites
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 18+"
    exit 1
fi

if ! command -v npm &> /dev/null; then
    echo "❌ npm is not installed. Please install npm"
    exit 1
fi

# Check for Android SDK
if [ -z "$ANDROID_HOME" ]; then
    echo "⚠️  Warning: ANDROID_HOME not set. Android Studio SDK required."
    echo "   Install Android Studio and set ANDROID_HOME environment variable"
    echo "   Example: export ANDROID_HOME=\$HOME/Android/Sdk"
fi

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

echo ""
echo "📦 Step 1: Installing dependencies..."
npm install

echo ""
echo "🔧 Step 2: Installing Capacitor for Android..."
npm install @capacitor/core @capacitor/cli @capacitor/android

echo ""
echo "🏗️  Step 3: Building frontend..."
cd frontend
npm install
npm run build
cd "$PROJECT_ROOT"

echo ""
echo "⚙️  Step 4: Initializing Capacitor..."
if [ ! -f "capacitor.config.json" ]; then
    npx cap init "StreamFlow" "com.streamflow.iptv" --web-dir=frontend/dist
fi

echo ""
echo "📱 Step 5: Adding Android platform..."
if [ ! -d "android" ]; then
    npx cap add android
else
    echo "   Android platform already exists, syncing..."
    npx cap sync android
fi

echo ""
echo "🎨 Step 6: Generating Android assets..."
# Copy icons to Android resources
if [ -d "frontend/public/icons" ]; then
    echo "   Copying icons to Android project..."
    mkdir -p android/app/src/main/res/mipmap-{mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi}
    
    # Convert SVG to PNG if needed (requires imagemagick)
    if command -v convert &> /dev/null; then
        convert frontend/public/icons/icon-192x192.svg -resize 48x48 android/app/src/main/res/mipmap-mdpi/ic_launcher.png 2>/dev/null || true
        convert frontend/public/icons/icon-192x192.svg -resize 72x72 android/app/src/main/res/mipmap-hdpi/ic_launcher.png 2>/dev/null || true
        convert frontend/public/icons/icon-192x192.svg -resize 96x96 android/app/src/main/res/mipmap-xhdpi/ic_launcher.png 2>/dev/null || true
        convert frontend/public/icons/icon-192x192.svg -resize 144x144 android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png 2>/dev/null || true
        convert frontend/public/icons/icon-192x192.svg -resize 192x192 android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png 2>/dev/null || true
    fi
fi

echo ""
echo "🔐 Step 7: Configuring permissions..."
cat > android/app/src/main/AndroidManifest.xml.patch << 'EOF'
<!-- Add these permissions to AndroidManifest.xml -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
EOF

echo ""
echo "📋 Next steps:"
echo ""
echo "1. Open Android Studio:"
echo "   npx cap open android"
echo ""
echo "2. Build APK in Android Studio:"
echo "   Build → Build Bundle(s) / APK(s) → Build APK(s)"
echo ""
echo "3. Or build from command line:"
echo "   cd android && ./gradlew assembleDebug"
echo "   APK location: android/app/build/outputs/apk/debug/app-debug.apk"
echo ""
echo "4. For release build:"
echo "   cd android && ./gradlew assembleRelease"
echo ""
echo "✅ Android project setup complete!"
echo ""
echo "📚 Documentation:"
echo "   Capacitor: https://capacitorjs.com/docs/android"
echo "   Android: https://developer.android.com/studio/build/building-cmdline"
