name: Release

on:
  pull_request: { types: [opened, synchronize, ready_for_review] }

jobs:
  build:
    strategy:
      matrix:
        build-type: [release, insecure]
    name: Build ${{ matrix.build-type }}
    if: github.event.pull_request.draft == false
    uses: ./.github/workflows/build-apk.yml
    with:
      version-name: ${{ github.sha }}
      build-type: ${{ matrix.build-type }}
    secrets:
      google-services-json: ${{ secrets.GOOGLE_SERVICES_JSON }}
      signing-key-store-base64: ${{ secrets.SIGNING_KEY_STORE_BASE64 }}
      signing-key-alias: ${{ secrets.SIGNING_KEY_ALIAS }}
      signing-key-password: ${{ secrets.SIGNING_KEY_PASSWORD }}
      signing-store-password: ${{ secrets.SIGNING_STORE_PASSWORD }}

  release:
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == false
    needs: [build]
    steps:
      - name: Download release artifacts
        uses: actions/download-artifact@v4
        with:
          name: release-artifacts

      - name: Download insecure artifacts
        uses: actions/download-artifact@v4
        with:
          name: insecure-artifacts

      - name: Upload to S3
        uses: capcom6/upload-s3-action@master
        env:
          AWS_REGION: ${{ secrets.AWS_REGION }}
        with:
          aws_key_id: ${{ secrets.AWS_KEY_ID }}
          aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY}}
          aws_bucket: ${{ secrets.AWS_BUCKET }}
          endpoint: ${{ secrets.AWS_ENDPOINT }}
          source_files: |
            ./apk/release/app-release.apk
            ./bundle/release/app-release.aab
            ./apk/insecure/app-insecure.apk
            ./bundle/insecure/app-insecure.aab
          destination_dir: apk/${{ github.event.pull_request.head.sha }}

      - name: Find Comment
        uses: peter-evans/find-comment@v3
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: "github-actions[bot]"
          body-includes: Pull request artifacts

      - name: Create or update comment
        uses: peter-evans/create-or-update-comment@v4
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            ## 🤖 Pull request artifacts

            | file | commit |
            | ---- | ------ |
            | [`app-release.apk`](https://s3.sms-gate.app/apk/${{ github.event.pull_request.head.sha }}/app-release.apk) | ${{ github.event.pull_request.head.sha }} |
            | [`app-release.aab`](https://s3.sms-gate.app/apk/${{ github.event.pull_request.head.sha }}/app-release.aab) | ${{ github.event.pull_request.head.sha }} |
            | [`app-insecure.apk`](https://s3.sms-gate.app/apk/${{ github.event.pull_request.head.sha }}/app-insecure.apk) | ${{ github.event.pull_request.head.sha }} |
            | [`app-insecure.aab`](https://s3.sms-gate.app/apk/${{ github.event.pull_request.head.sha }}/app-insecure.aab) | ${{ github.event.pull_request.head.sha }} |

          edit-mode: replace
