name: Android Prod Release

on:
  workflow_dispatch:
    inputs:
      tramline-input:
        description: "Tramline input"
        required: false

permissions:
  contents: write
  pull-requests: write

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      TERM: dumb
      ORG_GRADLE_PROJECT_READER_KEYSTORE_PASSWORD: ${{ secrets.READER_KEYSTORE_PASSWORD }}
      ORG_GRADLE_PROJECT_READER_KEY_PASSWORD: ${{ secrets.READER_KEY_PASSWORD }}
      BUGSNAG_API_KEY: ${{ secrets.BUGSNAG_API_KEY }}
    steps:
      - name: Configure Tramline
        id: tramline
        uses: tramlinehq/deploy-action@v0.1.7
        with:
          input: ${{ github.event.inputs.tramline-input }}

      - name: Setup versionName regardless of how this action is triggered
        id: version_name
        run: |
          WORKFLOW_INPUT=${{ steps.tramline.outputs.version_name }}
          VERSION_NAME=${WORKFLOW_INPUT:-"1.0.0"}
          echo "ORG_GRADLE_PROJECT_VERSION_NAME=$VERSION_NAME" >> $GITHUB_ENV

      - name: Setup versionCode regardless of how this action is triggered
        id: version_code
        run: |
          WORKFLOW_INPUT=${{ steps.tramline.outputs.version_code }}
          VERSION_CODE=${WORKFLOW_INPUT:-"1"}
          echo "ORG_GRADLE_PROJECT_VERSION_CODE=$VERSION_CODE" >> $GITHUB_ENV

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: zulu
          cache: 'gradle'

      - name: Decrypt secrets
        run: gpg --batch --yes --quiet --decrypt --passphrase=${{ secrets.KEYSTORE_ENCRYPT_KEY }} --output release/reader.jks release/reader.gpg

      - name: Build release artifacts
        run: |
          ./gradlew --no-daemon androidApp:bundleFullRelease androidApp:assembleFullRelease -Ptwine.isFoss=false
          ./gradlew --no-daemon androidApp:assembleFossRelease -Ptwine.isFoss=true

      - name: Upload Full Release Bundle
        uses: actions/upload-artifact@v7
        with:
          name: full-release.aab
          path: androidApp/build/outputs/bundle/fullRelease/androidApp-full-release.aab

      - name: Upload Full Release APK
        uses: actions/upload-artifact@v7
        with:
          name: full-release.apk
          path: androidApp/build/outputs/apk/full/release/androidApp-full-release.apk

      - name: Upload FOSS Release APK
        uses: actions/upload-artifact@v7
        with:
          name: foss-release.apk
          path: androidApp/build/outputs/apk/foss/release/androidApp-foss-release.apk

      - name: Clean secrets
        run: |
          rm -f release/reader.jks
