#!/bin/bash

# Build ProofMode AAR for Android
# This script builds the Android Archive (AAR) containing native libraries and Kotlin bindings

set -e

echo "🔨 Building ProofMode AAR for Android"

# Check prerequisites
if [ ! -d "target/android/jniLibs" ]; then
    echo "❌ Native libraries not found. Run ./scripts/setup-android.sh first"
    exit 1
fi

# Create AAR directory structure
echo "📦 Creating AAR structure..."
AAR_DIR="target/aar"
rm -rf "$AAR_DIR"
mkdir -p "$AAR_DIR/src/main"

# Copy native libraries
echo "📱 Copying native libraries..."
cp -r target/android/jniLibs "$AAR_DIR/src/main/"

# Copy Kotlin sources
echo "🔧 Copying Kotlin sources..."
cp -r bindings/android/* "$AAR_DIR/src/main/java/"

# Create AndroidManifest.xml
echo "📋 Creating AndroidManifest.xml..."
cat > "$AAR_DIR/src/main/AndroidManifest.xml" << 'MANIFEST_EOF'
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.guardianproject.proofmode">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34" />
    
    <!-- Required permissions -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
MANIFEST_EOF

# Create build.gradle for AAR
echo "⚙️ Creating build.gradle..."
cat > "$AAR_DIR/build.gradle" << 'GRADLE_EOF'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 34
    
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 1
        versionName "0.4.0"
        
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
    
    // Testing
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
GRADLE_EOF

# Create proguard rules
echo "🛡️ Creating ProGuard rules..."
cat > "$AAR_DIR/proguard-rules.pro" << 'PROGUARD_EOF'
# Add project specific ProGuard rules here.
# Keep ProofMode native methods
-keep class org.guardianproject.proofmode.** { *; }

# Keep native method names
-keepclasseswithmembernames class * {
    native <methods>;
}
PROGUARD_EOF

echo "✅ AAR structure created at $AAR_DIR"
echo ""
echo "To build the AAR:"
echo "1. Open the project in Android Studio"
echo "2. Run './gradlew assembleRelease' in the AAR directory"
echo "3. The AAR will be generated in $AAR_DIR/build/outputs/aar/"
echo ""
echo "Or use Gradle directly:"
echo "cd $AAR_DIR && ./gradlew assembleRelease"
