name: Release to Google Play
on:
  workflow_dispatch:
    inputs:
      inAppUpdatePriority:
        description: 'In app update priority (0-5)'
        type: number
        default: "0"
        required: false

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}

jobs:
  publish-google-play-release:
    runs-on: macos-14
    steps:
    - name: Setup build environment
      uses: msfjarvis/compose-lobsters/.github/reusable-workflows/setup-gradle@main
      with:
        token: ${{ secrets.POST_RELEASE_GH_TOKEN }}
        fetch-depth: 0
        cache-read-only: ${{ github.ref != 'refs/heads/main' }}

    - name: Set up Git author
      shell: bash
      run: |
        git config user.name "GitHub Actions"
        git config user.email noreply@github.com

    - name: Decrypt secrets
      run: |
        ./scripts/setup-age.sh
        ./scripts/signing-setup.sh "$AGE_SECRET_KEY"
      env:
        AGE_SECRET_KEY: ${{ secrets.AGE_SECRET_KEY }}

    - name: Get current version
      shell: bash
      run: |
        ./gradlew -q --no-configuration-cache clearPreRelease
        VERSION="$(tail -n1 android/version.properties | cut -d = -f 2)"
        echo VERSION="${VERSION}" >> $GITHUB_ENV

    - name: Update changelog
      uses: msfjarvis/keep-a-changelog-new-release@78cddbe3549f1c9263a2d754446d3765e865c518 # v2.1.1
      with:
        tag: v${{ env.VERSION }}
        version: ${{ env.VERSION }}

    - name: Commit changes
      shell: bash
      run: |
        git commit -am 'feat(release): bump version'

    - name: Build release assets
      shell: bash
      env:
        SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
        SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
      run: |
        ./gradlew --no-configuration-cache collectReleaseApks collectReleaseBundle -PenableSentry

    - name: Clean secrets
      run: scripts/signing-cleanup.sh

    - name: Get changelog entry
      id: changelog_reader
      uses: mindsers/changelog-reader-action@32aa5b4c155d76c94e4ec883a223c947b2f02656 # v2.2.3
      with:
        version: ${{ env.VERSION }}
        path: ./CHANGELOG.md

    - name: Write release notes file
      shell: bash
      env:
        CHANGELOG: ${{ steps.changelog_reader.outputs.changes }}
      run: |
        mkdir -p distribution/whatsnew
        printf '%s' "${CHANGELOG}" >> distribution/whatsnew/whatsnew-en-GB

    - name: Publish bundle to Google Play
      uses: r0adkll/upload-google-play@935ef9c68bb393a8e6116b1575626a7f5be3a7fb # v1.1.3
      with:
        mappingFile: android/bundle/mapping.txt
        packageName: dev.msfjarvis.claw.android
        releaseFiles: android/bundle/*.aab
        serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
        status: completed
        track: production
        whatsNewDirectory: distribution/whatsnew
        inAppUpdatePriority: ${{ inputs.inAppUpdatePriority }}

    - name: Post-release work
      shell: bash
      env:
        GITHUB_TOKEN: ${{ secrets.POST_RELEASE_GH_TOKEN }}
      run: |
        # Tag the current version
        git tag "v${VERSION}" -F distribution/whatsnew/whatsnew-en-GB

        # Push the tag to GitHub
        git push origin "v${VERSION}"

        # Create a GitHub release
        gh release create "v${VERSION}" --notes-file distribution/whatsnew/whatsnew-en-GB --title "v${VERSION}" ./android/apk/*.apk ./android/bundle/*.aab

        # Start the next development iteration
        ./gradlew -q --no-configuration-cache bumpSnapshot
        truncate -s 0 distribution/whatsnew/whatsnew-en-GB # Clear changelog
        git commit -am 'feat(release): start next development iteration'
        git push -u origin
