name: Deploy to Play Store and Firebase

#on:
#  push:
#    tags:
#      - 'a[0-9]+.[0-9]+.[0-9]+'
on: workflow_dispatch

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'
      - name: Generate Release Bundle
        run: ./gradlew bundleRelease

      - name: Sign Free Bundle
        uses: r0adkll/sign-android-release@v1
        # ID used to access action output
        id: sign_free_app
        with:
          releaseDirectory: app/build/outputs/bundle/freeRelease
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}

      - name: Sign Paid Bundle
        uses: r0adkll/sign-android-release@v1
        # ID used to access action output
        id: sign_paid_app
        with:
          releaseDirectory: app/build/outputs/bundle/paidRelease
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}

      - uses: actions/upload-artifact@v4
        with:
          name: free_release_bundle
          path: ${{steps.sign_free_app.outputs.signedReleaseFile}}

      - uses: actions/upload-artifact@v4
        with:
          name: free_mapping
          path: app/build/outputs/mapping/freeRelease/mapping.txt

      - uses: actions/upload-artifact@v4
        with:
          name: paid_release_bundle
          path: ${{steps.sign_paid_app.outputs.signedReleaseFile}}

      - uses: actions/upload-artifact@v4
        with:
          name: paid_mapping
          path: app/build/outputs/mapping/paidRelease/mapping.txt

      - uses: actions/upload-artifact@v4
        with:
          name: distribution
          path: distribution

  deploy-free-play-store:
    needs: [ build ]
    runs-on: ubuntu-latest
    steps:
      # 1
      - uses: actions/download-artifact@v4
        with:
          name: free_release_bundle
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}

      - uses: actions/download-artifact@v4
        with:
          name: free_mapping
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}

      - uses: actions/download-artifact@v4
        with:
          name: distribution
      - name: Display structure of downloaded files
        run: ls -R
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}
      # 2
      - name: Publish to Play Store internal test track
        uses: r0adkll/upload-google-play@v1.1.3
        with:
          serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
          # 3
          packageName: com.mshdabiola.ludo
          releaseFiles: app-free-release.aab
          track: internal
          status: completed
          inAppUpdatePriority: 2
          mappingFile: mapping.txt
          whatsNewDirectory: whatsnew

  deploy-paid-play-store:
    needs: [ build ]
    runs-on: ubuntu-latest
    steps:
      # 1
      - uses: actions/download-artifact@v4
        with:
          name: paid_release_bundle
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}

      - uses: actions/download-artifact@v4
        with:
          name: paid_mapping
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}

      - uses: actions/download-artifact@v4
        with:
          name: distribution
      - name: Display structure of downloaded files
        run: ls -R
      - name: 'Echo download path'
        run: echo ${{steps.download.outputs.download-path}}
      # 2
      - name: Publish to Play Store internal test track
        uses: r0adkll/upload-google-play@v1.1.3
        with:
          serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
          # 3
          packageName: com.mshdabiola.ludo.paid
          releaseFiles: app-paid-release.aab
          track: internal
          status: completed
          inAppUpdatePriority: 2
          mappingFile: mapping.txt
          whatsNewDirectory: whatsnew


