name: Build Android APK

on:
  workflow_dispatch:

jobs:
  build-android-apk:
    runs-on: ubuntu-latest
    container: node:22

    steps:
      - name: Checkout
        uses: https://github.com/actions/checkout@v4

      - name: Restore npm cache
        uses: https://github.com/actions/cache@v4
        with:
          path: /root/.npm
          key: npm-android-${{ hashFiles('package-lock.json') }}
          restore-keys: npm-android-

      - name: Restore Gradle cache
        uses: https://github.com/actions/cache@v4
        with:
          path: |
            /root/.gradle/caches
            /root/.gradle/wrapper
          key: gradle-android-${{ hashFiles('toju-app/android/**/*.gradle*', 'toju-app/android/gradle/wrapper/gradle-wrapper.properties') }}
          restore-keys: gradle-android-

      - name: Install Android build toolchain
        run: |
          apt-get update
          apt-get install -y --no-install-recommends wget unzip ca-certificates gnupg

          # node:22 is Debian Bookworm — openjdk-21-jdk is not in default repos.
          install -d /etc/apt/keyrings
          wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor -o /etc/apt/keyrings/adoptium.gpg
          echo "deb [signed-by=/etc/apt/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb bookworm main" > /etc/apt/sources.list.d/adoptium.list
          apt-get update
          apt-get install -y --no-install-recommends temurin-21-jdk

          export ANDROID_SDK_ROOT=/opt/android-sdk
          mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools"
          cd /tmp
          wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
          unzip -q commandlinetools-linux-11076708_latest.zip
          mv cmdline-tools "$ANDROID_SDK_ROOT/cmdline-tools/latest"
          export PATH="$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools"

          yes | sdkmanager --licenses >/dev/null
          sdkmanager "platform-tools" "platforms;android-36" "build-tools;35.0.0"

          echo "ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT" >> "$GITHUB_ENV"
          echo "ANDROID_HOME=$ANDROID_SDK_ROOT" >> "$GITHUB_ENV"
          echo "JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64" >> "$GITHUB_ENV"
          echo "PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools" >> "$GITHUB_ENV"

      - name: Install dependencies
        env:
          NODE_ENV: development
        run: npm ci

      - name: Resolve release version
        id: version
        run: node tools/resolve-release-version.js --write-output

      - name: Ensure draft release exists
        id: release
        env:
          GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: >
          node tools/gitea-release.js ensure-draft
          --server-url "${{ github.server_url }}"
          --repository "${{ github.repository }}"
          --tag "${{ steps.version.outputs.release_tag }}"
          --target "${{ github.sha }}"
          --name "${{ steps.version.outputs.release_name }}"
          --body "Automated draft release from ${{ github.ref_name }} @ ${{ github.sha }}"
          --write-output

      - name: Build debug APK
        run: bash tools/build-android-apk.sh

      - name: Stage Android APK
        run: |
          mkdir -p dist-android
          cp toju-app/android/app/build/outputs/apk/debug/app-debug.apk \
            "dist-android/Toju-${{ steps.version.outputs.release_version }}-android-debug.apk"

      - name: Upload Android APK to draft release
        env:
          GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: >
          node tools/gitea-release.js upload-built-assets
          --server-url "${{ github.server_url }}"
          --repository "${{ github.repository }}"
          --release-id "${{ steps.release.outputs.release_id }}"
          --dist-android dist-android
