name: "Android Stage"

on:
  push:
    branches: [ "qa" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Clone repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0 # we need the git history for the build number

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          flutter-version: 3.35.4

      - uses: actions/setup-java@v4
        with:
          distribution: "corretto"
          java-version: '21'

      - name: Download dotenv
        env:
          DOTENV_FILE: ${{ secrets.DOTENV_STAGE }}
          FILE_NAME: .stage.env
        run: >-
          sh .github/workflows/scripts/all_download_dotenv.sh
          $DOTENV_FILE
          $FILE_NAME

      - name: Flutterfire
        run: |
          sh .github/workflows/scripts/all_install_flutterfire.sh

      - name: pigeon
        run: |
          sh .github/workflows/scripts/all_run_pigeon.sh

      - name: Download Keystore Files
        env:
          KEYSTORE_FILE: ${{ secrets.KEYSTORE_FILE }}
          KEYSTORE_PROPS: ${{ secrets.KEYSTORE_PROPERTIES }}
        run: |
          KEYSTORE_PATH=comwell_key_app/android/comwell_keystore.jks
          PROPERTIES_PATH=comwell_key_app/android/key.properties
          echo "$KEYSTORE_FILE" | base64 -d > $KEYSTORE_PATH
          echo "$KEYSTORE_PROPS" | base64 -d > $PROPERTIES_PATH

      - name: flutter build aab
        env:
          FLAVOR: stage
        run: |
          sh .github/workflows/scripts/android_build_aab.sh $FLAVOR 

      - id: upload-google
        name: Upload Google Play Console
        uses: r0adkll/upload-google-play@v1.1.3
        with:
          serviceAccountJsonPlainText: ${{ secrets.GOOGLE_SERVICE_ACCOUNT }}
          packageName: com.comwell.phoenix.stage
          releaseFiles: comwell_key_app/build/app/outputs/bundle/StageRelease/app-Stage-release.aab
          changesNotSentForReview: false
          track: internal
          status: draft