#!/bin/bash

# Setup Android Studio project with ProofMode AAR
# Run this after building the AAR

set -e

echo "📱 Setting up Android Studio project with ProofMode"

AAR_PATH="./target/aar"
ANDROID_PROJECT_PATH="./android-example"

# Check if AAR exists
if [ ! -d "$AAR_PATH" ]; then
    echo "❌ AAR structure not found at $AAR_PATH"
    echo "Please run scripts/android/build-aar.sh first"
    exit 1
fi

# Copy AAR to Android project
echo "📦 Setting up AAR in Android project..."
mkdir -p "$ANDROID_PROJECT_PATH/app/libs"

# If we have a built AAR, copy it
if [ -f "$AAR_PATH/build/outputs/aar/aar-release.aar" ]; then
    cp "$AAR_PATH/build/outputs/aar/aar-release.aar" "$ANDROID_PROJECT_PATH/app/libs/proofmode.aar"
    echo "✅ Built AAR copied to Android project"
else
    echo "⚠️ No built AAR found. Creating placeholder."
    echo "Build the AAR first by running: cd $AAR_PATH && ./gradlew assembleRelease"
fi

# Update app build.gradle to include AAR
echo "🔧 Updating app build.gradle..."
GRADLE_FILE="$ANDROID_PROJECT_PATH/app/build.gradle"

# Add AAR dependency if not already present
if ! grep -q "implementation files('libs/proofmode.aar')" "$GRADLE_FILE"; then
    # Create backup
    cp "$GRADLE_FILE" "$GRADLE_FILE.backup"
    
    # Add AAR dependency
    sed -i '/dependencies {/a\    implementation files("libs/proofmode.aar")' "$GRADLE_FILE"
    echo "✅ AAR dependency added to build.gradle"
else
    echo "✅ AAR dependency already present in build.gradle"
fi

echo "✅ Android Studio project setup complete!"
echo ""
echo "Next steps:"
echo "1. Open android-example/ in Android Studio"
echo "2. Sync the project (Gradle sync)"
echo "3. Build and run the app"
echo ""
echo "If you encounter issues:"
echo "- Ensure the AAR is properly built"
echo "- Check that all native libraries are included"
echo "- Verify Kotlin bindings are correct"
