# Build a signed release APK and publish it as a Gitea/Forgejo release.
# Obtainium then tracks this repo's releases and offers updates.
#
# Triggers on any tag like v0.1.0. Requires a self-hosted Actions runner that has
# (or a container image that provides) the Android SDK + JDK 17 + Node 20.
#
# Required repo secrets (Settings -> Actions -> Secrets):
#   ANDROID_KEYSTORE_B64   base64 of your release keystore (keep this key forever —
#                          Obtainium/F-Droid pin the signer; a new key = users must reinstall)
#   ANDROID_KEYSTORE_PASSWORD
#   ANDROID_KEY_ALIAS
#   ANDROID_KEY_PASSWORD
#
# Generate the keystore once, locally:
#   keytool -genkeypair -v -keystore bigbrainparking.keystore \
#     -alias bigbrainparking -keyalg RSA -keysize 2048 -validity 10000
#   base64 -w0 bigbrainparking.keystore   # -> ANDROID_KEYSTORE_B64

name: build-apk
on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest
    # A container with Android SDK + Node preinstalled keeps the runner simple:
    container:
      image: reactnativecommunity/react-native-android:latest
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies
        run: npm install

      - name: Build the API client
        run: npm run build --workspace parksmarter-client

      - name: Restore signing keystore
        run: |
          echo "${{ secrets.ANDROID_KEYSTORE_B64 }}" | base64 -d > "$PWD/app/release.keystore"

      - name: Expo prebuild (generate android/)
        working-directory: app
        run: npx expo prebuild --platform android --no-install

      - name: Assemble signed release
        working-directory: app/android
        env:
          BBP_UPLOAD_STORE_FILE: ../release.keystore
          BBP_UPLOAD_STORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          BBP_UPLOAD_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          BBP_UPLOAD_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
        run: ./gradlew assembleRelease --no-daemon

      - name: Publish release with APK
        uses: akkuman/gitea-release-action@v1
        with:
          files: app/android/app/build/outputs/apk/release/*.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
