name: Flutter CICD

on:
  push:
    tags:
      - "v*"

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      KEY_JKS: ${{ secrets.KEY_JKS }}
      KEY_PATH: ${{ github.workspace }}/app/key.jks
      KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
      KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
    permissions:
      contents: write
    steps:
      - name: Free Disk Space
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: false
          android: false
          dotnet: true
          haskell: true
          large-packages: false
          docker-images: false
          swap-storage: false

      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Determine build type
        id: build-type
        run: |
          if [[ "${{ github.ref_name }}" =~ (pre|alpha) ]]; then
            echo "is_prerelease=true" >> $GITHUB_OUTPUT
            echo "Build type: Prerelease (APK only)"
          else
            echo "is_prerelease=false" >> $GITHUB_OUTPUT
            echo "Build type: Full release (APK + Bundle + Play Console)"
          fi

      - uses: ruby/setup-ruby@v1
        if: steps.build-type.outputs.is_prerelease == 'false'
        with:
          ruby-version: '3.3'
          bundler-cache: true
          working-directory: app

      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Download bundle dependencies
        if: steps.build-type.outputs.is_prerelease == 'false'
        run: |
          gem install bundler
          bundle config path vendor/bundle
          bundle install

      - name: Build container proxy
        working-directory: packages/flutter_mozilla_components/javascript/container_proxy
        run: |
          npm ci
          npx webpack

      - name: Create key file
        working-directory: app
        run: |
          echo $KEY_JKS | base64 -di > key.jks
          chmod 600 key.jks

      - uses: subosito/flutter-action@v2
        with:
          channel: stable
          flutter-version: 3.35.3
          cache: true

      - name: Setup Flutter dependencies
        working-directory: app
        run: |
          dart pub global activate melos
          melos bootstrap

      - name: Build APK
        working-directory: app
        run: flutter build apk --release --split-per-abi --no-tree-shake-icons

      - name: Build App Bundle
        if: steps.build-type.outputs.is_prerelease == 'false'
        working-directory: app
        run: flutter build appbundle --no-tree-shake-icons

      - name: Create github artifact release (Prerelease)
        if: steps.build-type.outputs.is_prerelease == 'true'
        uses: ncipollo/release-action@v1.17.0
        with:
          artifacts: "app/build/app/outputs/apk/release/*.apk"
          prerelease: true

      - name: Create github artifact release (Full)
        if: steps.build-type.outputs.is_prerelease == 'false'
        uses: ncipollo/release-action@v1.17.0
        with:
          artifacts: "app/build/app/outputs/apk/release/*.apk,app/build/app/outputs/bundle/release/app-release.aab"

      - name: Upload app bundle artifact
        if: steps.build-type.outputs.is_prerelease == 'false'
        uses: actions/upload-artifact@v4
        with:
          name: appbundle
          path: app/build/app/outputs/bundle/release/app-release.aab

      - name: Release to Google Play (internal)
        if: steps.build-type.outputs.is_prerelease == 'false'
        working-directory: app
        env:
          SUPPLY_PACKAGE_NAME: ${{ secrets.ANDROID_PACKAGE_NAME }}
          SUPPLY_JSON_KEY_DATA: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}
        run: |
          bundle exec fastlane supply \
          --aab build/app/outputs/bundle/release/app-release.aab \
          --track internal

      - name: Cleanup
        if: always()
        working-directory: app
        run: rm -f key.jks
