#!/bin/bash

# LuckyChit Android Build Script
# Builds both APK and App Bundle for distribution

echo "🤖 Building Android Release"
echo "============================"
echo ""

# Check if we're in the right directory
if [ ! -f "pubspec.yaml" ]; then
    echo "❌ Error: pubspec.yaml not found!"
    echo "Please run this script from the luckychit directory"
    exit 1
fi

# Get current version
VERSION=$(grep "^version:" pubspec.yaml | sed 's/version: //')
echo "📱 App Version: $VERSION"
echo ""

# Clean
echo "🧹 Step 1/4: Cleaning..."
flutter clean

# Get dependencies
echo "📦 Step 2/4: Getting dependencies..."
flutter pub get

# Build APK
echo "🔨 Step 3/4: Building APK (for direct distribution)..."
flutter build apk --release

if [ $? -ne 0 ]; then
    echo "❌ APK build failed!"
    exit 1
fi

echo "✅ APK build successful!"
echo ""

# Build App Bundle
echo "🔨 Step 4/4: Building App Bundle (for Play Store)..."
flutter build appbundle --release

if [ $? -ne 0 ]; then
    echo "❌ App Bundle build failed!"
    exit 1
fi

echo "✅ App Bundle build successful!"
echo ""

# Show results
echo "==========================================="
echo "🎉 Build Complete!"
echo ""
echo "📦 Output Files:"
echo ""
echo "   APK (Direct Distribution):"
echo "   📄 build/app/outputs/flutter-apk/app-release.apk"
APK_SIZE=$(du -h build/app/outputs/flutter-apk/app-release.apk | cut -f1)
echo "   📊 Size: $APK_SIZE"
echo ""
echo "   App Bundle (Google Play Store):"
echo "   📄 build/app/outputs/bundle/release/app-release.aab"
AAB_SIZE=$(du -h build/app/outputs/bundle/release/app-release.aab | cut -f1)
echo "   📊 Size: $AAB_SIZE"
echo ""
echo "📤 Next Steps:"
echo ""
echo "   For Direct Distribution (APK):"
echo "   1. Upload APK to your server or file host"
echo "   2. Share download link with users"
echo "   3. Users enable 'Install from Unknown Sources'"
echo "   4. Users download and install"
echo ""
echo "   For Google Play Store (AAB):"
echo "   1. Go to https://play.google.com/console"
echo "   2. Select your app"
echo "   3. Go to Release → Production"
echo "   4. Create new release"
echo "   5. Upload app-release.aab"
echo "   6. Add release notes"
echo "   7. Review and roll out"
echo ""
echo "📋 Version: $VERSION"
echo "   Update version in pubspec.yaml for next release"
echo ""

