#!/bin/bash

# Test Android AAR build
# This script tests that the Android setup is working correctly

set -e

echo "🧪 Testing ProofMode Android Build"
echo "=================================="

AAR_DIR="target/aar"

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

echo "✅ AAR directory structure found"

# Check for native libraries
echo "🔍 Checking for native libraries..."
if [ -f "$AAR_DIR/src/main/jniLibs/arm64-v8a/libproofmode.so" ]; then
    echo "✅ ARM64 library found"
else
    echo "⚠️ ARM64 library not found (expected for mock build)"
fi

if [ -f "$AAR_DIR/src/main/jniLibs/armeabi-v7a/libproofmode.so" ]; then
    echo "✅ ARMv7 library found"
else
    echo "⚠️ ARMv7 library not found (expected for mock build)"
fi

# Check for Kotlin sources
echo "🔍 Checking for Kotlin sources..."
if [ -f "$AAR_DIR/src/main/java/guardianproject/proofmode/ProofMode.kt" ]; then
    echo "✅ Kotlin bindings found"
else
    echo "❌ Kotlin bindings not found"
    exit 1
fi

# Check for Android manifest
if [ -f "$AAR_DIR/src/main/AndroidManifest.xml" ]; then
    echo "✅ AndroidManifest.xml found"
else
    echo "❌ AndroidManifest.xml not found"
    exit 1
fi

# Check for build configuration
if [ -f "$AAR_DIR/build.gradle" ]; then
    echo "✅ build.gradle found"
else
    echo "❌ build.gradle not found"
    exit 1
fi

echo ""
echo "📋 AAR Structure Summary:"
echo "========================"
find "$AAR_DIR" -type f -name "*.so" -o -name "*.kt" -o -name "*.xml" -o -name "*.gradle" | sort

echo ""
echo "🎯 Integration Test: Android Example Project"
echo "============================================"

ANDROID_PROJECT="android-example"

# Check if Android example project exists
if [ ! -d "$ANDROID_PROJECT" ]; then
    echo "❌ Android example project not found at $ANDROID_PROJECT"
    exit 1
fi

echo "✅ Android example project found"

# Check for key Android project files
PROJECT_FILES=(
    "$ANDROID_PROJECT/app/src/main/java/org/proofmode/android/example/MainActivity.kt"
    "$ANDROID_PROJECT/app/src/main/java/org/proofmode/android/example/proofmode/ProofModeManager.kt"
    "$ANDROID_PROJECT/app/src/main/AndroidManifest.xml"
    "$ANDROID_PROJECT/app/build.gradle"
    "$ANDROID_PROJECT/build.gradle"
)

for file in "${PROJECT_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "✅ Found: $file"
    else
        echo "❌ Missing: $file"
    fi
done

echo ""
echo "📱 Android Project Structure:"
echo "============================="
echo "android-example/"
find "$ANDROID_PROJECT" -type f \( -name "*.kt" -o -name "*.xml" -o -name "*.gradle" \) | head -20 | sed 's/^/  /'

echo ""
echo "🔧 Build Test Results:"
echo "======================"

if command -v gradle &> /dev/null; then
    echo "✅ Gradle found: $(gradle --version | head -1)"
    
    # Test if we can at least validate the build files
    echo "🧪 Testing Gradle configuration..."
    cd "$AAR_DIR"
    if gradle tasks --quiet &> /dev/null; then
        echo "✅ AAR Gradle configuration is valid"
    else
        echo "⚠️ AAR Gradle configuration may have issues (Android SDK required)"
    fi
    cd - > /dev/null
    
else
    echo "⚠️ Gradle not found - cannot test builds"
    echo "   Install Gradle or Android Studio to build the AAR"
fi

echo ""
echo "📝 Next Steps:"
echo "=============="
echo "1. Install Android Studio with:"
echo "   - Android SDK (API 21-34)"
echo "   - Android NDK (for native builds)"
echo "   - Gradle (8.0+)"
echo ""
echo "2. Open android-example/ in Android Studio"
echo ""
echo "3. Build the AAR:"
echo "   cd target/aar"
echo "   ./gradlew assembleRelease"
echo ""
echo "4. Setup the Android project:"
echo "   ./scripts/android/setup-studio-project.sh"
echo ""
echo "🎉 Android setup validation complete!"

# Check for potential issues
echo ""
echo "🔍 Potential Issues Check:"
echo "=========================="

# Check permissions in AndroidManifest
if grep -q "CAMERA" "$ANDROID_PROJECT/app/src/main/AndroidManifest.xml"; then
    echo "✅ Camera permission declared"
else
    echo "⚠️ Camera permission may be missing"
fi

if grep -q "ACCESS_FINE_LOCATION" "$ANDROID_PROJECT/app/src/main/AndroidManifest.xml"; then
    echo "✅ Location permission declared"
else
    echo "⚠️ Location permission may be missing"
fi

# Check for ProofMode integration in MainActivity
if grep -q "ProofModeManager" "$ANDROID_PROJECT/app/src/main/java/org/proofmode/android/example/MainActivity.kt"; then
    echo "✅ ProofMode integration found in MainActivity"
else
    echo "⚠️ ProofMode integration may be missing in MainActivity"
fi

echo ""
echo "✨ Test complete! Your Android setup is ready for development."