name: Android CI (PR)

on:
  push:
    branches:
      - main
    paths-ignore:
      - "**.md"
      - ".idea/**"
      - ".gitignore"
      - ".gitmodules"
  pull_request:
    branches:
      - main
  workflow_dispatch:

concurrency:
  group: build-pr-${{ github.ref }}
  cancel-in-progress: true

jobs:

  build_and_detekt:
    if: github.repository == 'meshtastic/Meshtastic-Android' && !( github.head_ref == 'scheduled-updates' || github.head_ref == 'l10n_main' )
    uses: ./.github/workflows/reusable-android-build.yml
    secrets: inherit

  androidTest:
    # Assuming androidTest should also only run for the main repository
    if: github.repository == 'meshtastic/Meshtastic-Android' && !( github.head_ref == 'scheduled-updates' || github.head_ref == 'l10n_main' )
    uses: ./.github/workflows/reusable-android-test.yml
    with:
      api_levels: '[35]' # Run only on API 35 for PRs
      # upload_artifacts defaults to true, so no need to explicitly set
    secrets:
      GRADLE_ENCRYPTION_KEY: ${{ secrets.GRADLE_ENCRYPTION_KEY }}
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
          
  check-workflow-status:
    name: Check Workflow Status
    runs-on: ubuntu-latest
    needs:
      [
        build_and_detekt,
        androidTest
      ]
    if: always()
    steps:
      - name: Check Workflow Status
        run: |
          exit_on_result() {
            if [[ "$2" == "failure" || "$2" == "cancelled" ]]; then
              echo "Job '$1' failed or was cancelled."
              exit 1
            fi
          }
          exit_on_result "build_and_detekt" "${{ needs.build_and_detekt.result }}"
          exit_on_result "androidTest" "${{ needs.androidTest.result }}"
