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

name: Build and release apps to beta track

jobs:
  get-version:
    name: Get version
    runs-on: macos
    steps:
      - uses: https://gitea.com/actions/checkout@v4

      - name: Fetch tags from main branch
        # since this workflow is triggered by a pull request, we want to match the latest tag of the main branch
        id: version
        run: |
          git fetch origin main --tags
          LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
          # remove the 'v' prefix from the tag name
          echo "BUILD_NAME=${LATEST_TAG//v}" >> $GITHUB_OUTPUT

      - name: Output the version that is being used
        run: |
          echo "Building for version ${{ steps.version.outputs.BUILD_NAME }}"

    outputs:
      build_name: ${{ steps.version.outputs.BUILD_NAME }}

  build-android:
    name: Build and upload android app
    uses: ./.gitea/workflows/workflow_build-app-android.yaml
    with:
      build_type: beta
      build_name: ${{ needs.get-version.outputs.build_name }}
    secrets:
      ANDROID_SECRET_PROPERTIES_BASE64: ${{ secrets.ANDROID_SECRET_PROPERTIES_BASE64 }}
      ANDROID_GOOGLE_PLAY_JSON_BASE64: ${{ secrets.ANDROID_GOOGLE_PLAY_JSON_BASE64 }}
      ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
      ANDROID_GOOGLE_MAPS_API_KEY: ${{ secrets.ANDROID_GOOGLE_MAPS_API_KEY }}
    needs: get-version

  build-ios:
    name: Build and upload ios app
    uses: ./.gitea/workflows/workflow_build-app-ios.yaml
    with:
      build_type: beta
      build_name: ${{ needs.get-version.outputs.build_name }}
    secrets:
      IOS_ASC_KEY_ID: ${{ secrets.IOS_ASC_KEY_ID }}
      IOS_ASC_ISSUER_ID: ${{ secrets.IOS_ASC_ISSUER_ID }}
      IOS_ASC_KEY: ${{ secrets.IOS_ASC_KEY }}
      IOS_MATCH_REPO_SSH_KEY_BASE64: ${{ secrets.IOS_MATCH_REPO_SSH_KEY_BASE64 }}
      IOS_MATCH_PASSWORD: ${{ secrets.IOS_MATCH_PASSWORD }}
      IOS_GOOGLE_MAPS_API_KEY: ${{ secrets.IOS_GOOGLE_MAPS_API_KEY }}
    needs: build-android # technically not needed, but this prevents the builds from running in parallel
