name: Build and Publish F-Droid Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Android SDK
        run: |
          set -e
          export ANDROID_HOME=$HOME/android-sdk
          export ANDROID_SDK_ROOT=$ANDROID_HOME
          mkdir -p $ANDROID_HOME/cmdline-tools
          curl -sSL https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o /tmp/cmdline-tools.zip
          unzip -q /tmp/cmdline-tools.zip -d $ANDROID_HOME/cmdline-tools
          mv $ANDROID_HOME/cmdline-tools/cmdline-tools $ANDROID_HOME/cmdline-tools/latest
          export PATH="$ANDROID_HOME/cmdline-tools/latest/bin:$PATH"
          yes | sdkmanager --licenses >/dev/null 2>&1 || true
          sdkmanager "platform-tools" "platforms;android-36" "build-tools;36.0.0"
          echo "ANDROID_HOME=$ANDROID_HOME" >> $GITHUB_ENV
          echo "ANDROID_SDK_ROOT=$ANDROID_HOME" >> $GITHUB_ENV
          echo "$ANDROID_HOME/platform-tools" >> $GITHUB_PATH

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: ${{ runner.os }}-gradle-

      - name: Decode signing keystore
        run: |
          echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 --decode > /tmp/release.keystore
          echo "ANDROID_KEYSTORE_FILE=/tmp/release.keystore" >> $GITHUB_ENV
        env:
          ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}

      - name: Build F-Droid Release APK
        run: ./gradlew :app:assembleFdroidRelease --no-daemon --stacktrace
        env:
          ANDROID_KEYSTORE_FILE: /tmp/release.keystore
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}

      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: centralcloud-oncall-fdroid-release
          path: app/build/outputs/apk/fdroid/release/*.apk

      - name: Install Firebase CLI
        run: |
          curl -sL https://firebase.tools/bin/linux/v13.35.0 > /tmp/firebase
          chmod +x /tmp/firebase
          echo "/tmp" >> $GITHUB_PATH

      - name: Distribute to Firebase App Distribution
        run: |
          firebase appdistribution:distribute \
            app/build/outputs/apk/fdroid/release/*.apk \
            --app "${{ secrets.FIREBASE_APP_ID }}" \
            --groups "oncall-testers" \
            --release-notes "Build ${{ github.ref_name }} from ${{ github.sha }}" \
            --token "${{ secrets.FIREBASE_CI_TOKEN }}"

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: app/build/outputs/apk/fdroid/release/*.apk
          generate_release_notes: true
