on:
  pull_request:
    branches:
      - main
    paths:
      - frontend/**


name: Build and release debug APK

jobs:
  build:
    name: Build APK
    runs-on: ubuntu-latest
    steps:

    - name: Install prerequisites
      run: |
        apt-get update
        apt-get install -y jq

    - uses: https://gitea.com/actions/checkout@v4


    - uses: https://github.com/actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'zulu'

    - name: Fix flutter SDK folder permission
      run: git config --global --add safe.directory "*"

    - uses: https://github.com/subosito/flutter-action@v2
      with:
        channel: stable
        flutter-version: 3.22.0
        cache: true

    - name: Setup Android SDK
      uses: https://github.com/android-actions/setup-android@v3

    - run: flutter pub get
      working-directory: ./frontend

    - name: Add required secrets
      env:
        ANDROID_SECRETS_PROPERTIES: ${{ secrets.ANDROID_SECRETS_PROPERTIES }}
      run: |
        echo "$ANDROID_SECRETS_PROPERTIES" >> ./android/secrets.properties
      working-directory: ./frontend

    - name: Sanity check
      run: |
        ls
        ls -lah android
      working-directory: ./frontend

    - run: flutter build apk --debug --split-per-abi --build-number=${{ gitea.run_number }}
      working-directory: ./frontend

    - name: Upload APKs to artifacts
      uses: https://gitea.com/actions/upload-artifact@v3
      with:
        name: app-release
        path: frontend/build/app/outputs/flutter-apk/
        if-no-files-found: error
        retention-days: 15
