name: Android CI Build

on:
  workflow_dispatch:

permissions:
  contents: read

env:
  KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
  RELEASE_SIGN_KEY_ALIAS: ${{ secrets.RELEASE_SIGN_KEY_ALIAS }}
  RELEASE_SIGN_KEY_PASSWORD: ${{ secrets.RELEASE_SIGN_KEY_PASSWORD }}

jobs:
  build_apks:
    runs-on: ubuntu-24.04
    name: Build artifacts
    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: set up JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build and sign release APK
        run: ./gradlew assembleRelease

      - name: Find generated release APK name
        id: find_apk_release
        run: |
          APK_PATH=$(find app/build/outputs/apk/release -name "*.apk" | head -n 1)
          echo "APK_PATH=$APK_PATH" >> $GITHUB_ENV
          echo "APK_NAME=$(basename $APK_PATH)" >> $GITHUB_ENV

      - name: Upload APK with original name
        uses: actions/upload-artifact@v6
        with:
          name: ${{ env.APK_NAME }}
          path: ${{ env.APK_PATH }}

      - name: Build and sign debug APK
        run: ./gradlew assembleDebug

      - name: Find generated debug APK name
        id: find_apk_debug
        run: |
            APK_PATH=$(find app/build/outputs/apk/debug -name "*.apk" | head -n 1)
            echo "APK_PATH=$APK_PATH" >> $GITHUB_ENV
            echo "APK_NAME=$(basename $APK_PATH)" >> $GITHUB_ENV

      - name: Upload APK with original name
        uses: actions/upload-artifact@v6
        with:
          name: ${{ env.APK_NAME }}
          path: ${{ env.APK_PATH }}
