name: build
permissions:
  contents: read

on:
  workflow_dispatch:
    inputs:
      build_type:
        type: choice
        description: "Build type"
        required: true
        default: debug
        options:
          - debug
          - nightly
          - release
      flavor:
        type: choice
        description: "Product flavor"
        required: true
        default: fdroid
        options:
          - fdroid
          - standalone
    secrets:
      SIGNING_KEY_ALIAS:
        required: false
      SIGNING_KEY_PASSWORD:
        required: false
      SIGNING_STORE_PASSWORD:
        required: false
      SERVICE_ACCOUNT_JSON:
        required: false
      KEYSTORE:
        required: false
  workflow_call:
    inputs:
      build_type:
        type: string
        description: "Build type"
        required: true
        default: debug
      flavor:
        type: string
        description: "Product flavor"
        required: false
        default: fdroid
    secrets:
      SIGNING_KEY_ALIAS:
        required: false
      SIGNING_KEY_PASSWORD:
        required: false
      SIGNING_STORE_PASSWORD:
        required: false
      SERVICE_ACCOUNT_JSON:
        required: false
      KEYSTORE:
        required: false

env:
  UPLOAD_DIR_ANDROID: android_artifacts

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
      SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
      SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
      KEY_STORE_FILE: 'android_keystore.jks'
      KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
    outputs:
      UPLOAD_DIR_ANDROID: ${{ env.UPLOAD_DIR_ANDROID }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Set up JDK 17
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Decode Keystore
        id: decode_keystore
        uses: timheuer/base64-to-file@v2.0
        with:
          fileName: ${{ env.KEY_STORE_FILE }}
          fileDir: ${{ env.KEY_STORE_LOCATION }}
          encodedString: ${{ secrets.KEYSTORE }}
      - name: Create keystore path env var
        if: ${{ inputs.build_type != 'debug' }}
        run: |
          store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
          echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV

      - name: Build APK
        run: |
          flavor=${{ inputs.flavor }}
          build_type=${{ inputs.build_type }}
          case $build_type in
            "release")
              ./gradlew :app:assemble${flavor^}Release --info
              ;;
            "nightly")
              ./gradlew :app:assemble${flavor^}Nightly --info
              ;;
            "debug")
              ./gradlew :app:assemble${flavor^}Debug --stacktrace
              ;;
          esac
      - name: Get release apk path
        id: apk-path
        run: echo "path=$(find . -regex '^.*/build/outputs/apk/${{ inputs.flavor }}/${{ inputs.build_type }}/.*\.apk$' -type f | head -1 | tail -c+2)" >> $GITHUB_OUTPUT
      - name: Upload All APK Artifacts
        uses: actions/upload-artifact@v7
        with:
          name: android_artifacts_${{ inputs.flavor }}
          path: >-
            app/build/outputs/apk/${{ inputs.flavor }}/${{ inputs.build_type }}/*.apk
          retention-days: 1
          if-no-files-found: warn