name: Publish to Maven Central

on:
  # Runs automatically on release tags, but can also be triggered manually
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      ref:
        description: 'Git ref to publish (tag or branch)'
        required: true
        default: 'main'

permissions:
  contents: read

jobs:
  publish-maven:
    name: Build AAR and publish to Maven Central
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.inputs.ref || github.ref }}

      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17

      - name: Set up Android SDK
        uses: android-actions/setup-android@v3

      - name: Install Android NDK
        run: sdkmanager "ndk;27.0.12077973"

      - name: Install Rust Android targets
        run: |
          rustup target add aarch64-linux-android
          rustup target add armv7-linux-androideabi
          rustup target add x86_64-linux-android

      - name: Add NDK toolchain to PATH
        run: echo "$ANDROID_HOME/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH

      - name: Build native libraries
        working-directory: android
        run: ./gradlew buildAar
        env:
          ANDROID_NDK_HOME: ${{ env.ANDROID_HOME }}/ndk/27.0.12077973

      - name: Publish to Maven Central
        working-directory: android
        run: ./gradlew publishToMavenCentral
        env:
          SONATYPE_USERNAME: ${{ secrets.MAVEN_CENTRAL_USER }}
          SONATYPE_PASSWORD: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
          ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_SECRET_KEY }}
          ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.GPG_SECRET_PASSPHRASE }}
