name: Billing Sandbox

on:
  workflow_dispatch:
    inputs:
      scenario:
        description: Billing sandbox scenario to run
        type: choice
        default: restore
        options:
          - restore
          - purchase
  schedule:
    - cron: '47 8 * * *'

permissions:
  contents: read

jobs:
  billing-sandbox:
    name: Billing Sandbox
    runs-on: [self-hosted, android-billing-sandbox]
    timeout-minutes: 45
    concurrency:
      group: billing-sandbox-${{ github.ref }}
      cancel-in-progress: false
    env:
      BILLING_SCENARIO: ${{ github.event_name == 'schedule' && 'restore' || inputs.scenario }}
    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 upload keystore
        env:
          ENCODED_KEYSTORE: ${{ secrets.KEYSTORE_BASE64 }}
        run: |
          if [ -n "$ENCODED_KEYSTORE" ]; then
            echo "$ENCODED_KEYSTORE" | base64 -d > app/upload.jks
            echo "BILLING_SIGNING_READY=true" >> "$GITHUB_ENV"
          else
            echo "BILLING_SIGNING_READY=false" >> "$GITHUB_ENV"
          fi

      - name: Verify attached billing device
        run: |
          adb devices
          test "$(adb devices | awk 'NR > 1 && $2 == "device" { count++ } END { print count + 0 }')" -eq 1

      - name: Clear app data for restore scenario
        if: env.BILLING_SCENARIO == 'restore'
        run: adb shell pm clear com.yorvana || true

      - name: Build staging app and androidTest
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        run: |
          if [ "$BILLING_SIGNING_READY" = "true" ]; then
            export KEYSTORE_FILE=upload.jks
          fi
          ./gradlew :app:assembleStaging :app:assembleStagingAndroidTest -PbillingSandboxBuild

      - name: Run billing sandbox androidTest
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          BILLING_SANDBOX_ACCOUNT: ${{ secrets.BILLING_SANDBOX_ACCOUNT }}
        run: |
          if [ "$BILLING_SIGNING_READY" = "true" ]; then
            export KEYSTORE_FILE=upload.jks
          fi
          ./gradlew :app:connectedStagingAndroidTest -PbillingSandboxBuild \
            -Pandroid.testInstrumentationRunnerArguments.annotation=com.yorvana.testsupport.tiers.BillingSandbox \
            -Pandroid.testInstrumentationRunnerArguments.billingSandboxScenario=${BILLING_SCENARIO} \
            -Pandroid.testInstrumentationRunnerArguments.billingSandboxAccount=${BILLING_SANDBOX_ACCOUNT} \
            -Pandroid.testInstrumentationRunnerArguments.billingSandboxSigningReady=${BILLING_SIGNING_READY} \
            -Pandroid.testInstrumentationRunnerArguments.billingSandboxExternalDeviceReady=true

      - name: Upload billing sandbox reports
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: billing-sandbox-test-reports
          path: |
            app/build/reports/androidTests/connected/
            app/build/outputs/androidTest-results/connected/
          if-no-files-found: warn
          retention-days: 7
