on:
  workflow_call:
    inputs:
      build_type:
        description: 'Release type (release, beta)'
        required: true
        type: string
      build_name:
        description: 'Build name'
        required: true
        type: string
    secrets:
      ANDROID_SECRET_PROPERTIES_BASE64:
        required: true
      ANDROID_GOOGLE_PLAY_JSON_BASE64:
        required: true
      ANDROID_KEYSTORE_BASE64:
        required: true
      ANDROID_GOOGLE_MAPS_API_KEY:
        required: true

name: Build and release android appbundle to specfied track

defaults:
  run:
    working-directory: frontend/android

jobs:
  build:
    runs-on: macos-14
    env:
      # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
      BUNDLE_GEMFILE: ${{ gitea.workspace }}/frontend/android/Gemfile

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

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

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

    - 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-file: ${{ gitea.workspace }}/frontend/pubspec.yaml
        architecture: x64
        cache: true

    - name: Install dependencies and clean up
      run: |
        flutter pub get
        flutter clean

    - name: Set up ruby env and install fastlane
      uses: https://github.com/ruby/setup-ruby@v1
      with:
        ruby-version: 3.3
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically

    - name: Add required secret files
      run: |
        echo "${{ secrets.ANDROID_SECRET_PROPERTIES_BASE64 }}" | base64 -d > secrets.properties
        echo "${{ secrets.ANDROID_GOOGLE_PLAY_JSON_BASE64 }}" | base64 -d > google-key.json
        echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 -d > release.keystore

    - name: Run fastlane lane
      run: bundle exec fastlane deploy_${{ inputs.build_type }}
      env:
        BUILD_NUMBER: ${{ gitea.run_number }}
        BUILD_NAME: ${{ inputs.build_name }}
        ANDROID_GOOGLE_MAPS_API_KEY: ${{ secrets.ANDROID_GOOGLE_MAPS_API_KEY }}
