name: Deploy to Play Store

on:
  workflow_dispatch:
    inputs:
      track:
        description: Play Store release track
        type: choice
        default: internal
        options:
          - internal
          - beta
          - production
      status:
        description: Play Store release status
        type: choice
        default: draft
        options:
          - draft
          - inProgress
          - completed

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Set up JDK 17
        uses: actions/setup-java@v5
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v6
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/master' }}
          cache-cleanup: on-success

      - name: Decode Keystore
        env:
          ENCODED_KEYSTORE: ${{ secrets.KEYSTORE_BASE64 }}
        run: echo "$ENCODED_KEYSTORE" | base64 -d > app/upload.jks

      - name: Build Release AAB
        env:
          KEYSTORE_FILE: upload.jks
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        run: ./gradlew bundleRelease

      - name: Upload to Play Store
        uses: r0adkll/upload-google-play@v1
        with:
          serviceAccountJsonPlainText: ${{ secrets.PLAY_CONFIG_JSON }}
          packageName: com.yorvana
          releaseFiles: app/build/outputs/bundle/release/app-release.aab
          track: ${{ inputs.track }}
          status: ${{ inputs.status }}
          whatsNewDirectory: distribution/whatsnew
