name: sample-distribution

concurrency:
  group: sample-distribution-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    paths:
      - '.github/workflows/sample-distribution.yml'
      - 'package/**'
      - 'examples/SampleApp/**'

permissions:
  actions: write
  contents: read

jobs:
  build_and_deploy_ios_testflight_qa:
    name: Build SampleApp iOS and Deploy-${{ github.ref == 'refs/heads/develop' }}
    runs-on: [macos-15]
    strategy:
      matrix:
        node-version: [24.x]
    steps:
      - name: Connect Bot
        uses: webfactory/ssh-agent@v0.10.0
        with:
          ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
      - uses: actions/checkout@v6
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'yarn'
          cache-dependency-path: 'yarn.lock'
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: '26.2' # Update as needed
      - uses: ./.github/actions/ruby-cache
      - name: Install && Build - SDK and Sample App
        uses: ./.github/actions/install-and-build-sdk
      - name: Cache iOS pods
        uses: actions/cache@v5
        with:
          path: examples/SampleApp/ios/Pods
          key: ${{ runner.os }}-pods-${{ hashFiles('examples/SampleApp/ios/Podfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-pods-
      - name: iOS Pods setup
        working-directory: examples/SampleApp/ios
        run: |
          rm -rf Pods
          rm -f Podfile.lock
          rm -rf build
          rm -rf ~/Library/Developer/Xcode/DerivedData
          bundle exec pod install
      - name: Build and release Testflight QA
        working-directory: examples/SampleApp
        run: bundle exec fastlane ios deploy_to_testflight_qa deploy:${{ github.ref == 'refs/heads/develop' }};
        env:
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
          MATCH_GIT_BASIC_AUTHORIZATION: ${{ secrets.MATCH_GIT_BASIC_AUTHORIZATION }}
          APPSTORE_API_KEY: ${{ secrets.APPSTORE_API_KEY }}

  build_and_deploy_android_firebase:
    name: Build SampleApp Android and Deploy-${{ github.ref == 'refs/heads/develop' }}
    runs-on: public
    strategy:
      matrix:
        node-version: [24.x]
    steps:
      - uses: actions/checkout@v6
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'yarn'
          cache-dependency-path: 'yarn.lock'
      - uses: actions/setup-java@v5
        with:
          distribution: 'zulu'
          java-version: '17'
          check-latest: true
      - name: Setup Android SDK
        uses: amyu/setup-android@v5

      - uses: ./.github/actions/ruby-cache
      - name: Install && Build - SDK and Sample App
        uses: ./.github/actions/install-and-build-sdk
      - name: Build and deploy Android Firebase
        working-directory: examples/SampleApp
        run: bundle exec fastlane android firebase_build_and_upload deploy:${{ github.ref == 'refs/heads/develop' }};
        env:
          ANDROID_FIREBASE_APP_ID: ${{ secrets.ANDROID_FIREBASE_APP_ID }}
          FIREBASE_CREDENTIALS_JSON: ${{ secrets.FIREBASE_CREDENTIALS_JSON }}
      # Publish the same APK to S3 for the public download link. Reuses the
      # artifact the Fastlane step above already built (app-build/), no rebuild.
      - name: Ensure AWS CLI is available
        if: github.ref == 'refs/heads/develop'
        # The self-hosted `public` runner does not ship the AWS CLI. Install v2
        # only when missing, to a user-writable dir (no sudo), then expose it.
        run: |
          if command -v aws >/dev/null 2>&1; then
            echo "AWS CLI already installed: $(aws --version)"
            exit 0
          fi
          echo "AWS CLI not found; installing v2..."
          case "$(uname -m)" in
            x86_64) zip="awscli-exe-linux-x86_64.zip" ;;
            aarch64|arm64) zip="awscli-exe-linux-aarch64.zip" ;;
            *) echo "Unsupported architecture: $(uname -m)"; exit 1 ;;
          esac
          curl -fsSL "https://awscli.amazonaws.com/${zip}" -o /tmp/awscliv2.zip
          unzip -q /tmp/awscliv2.zip -d /tmp
          /tmp/aws/install --bin-dir "$HOME/.local/bin" --install-dir "$HOME/.local/aws-cli" --update
          echo "$HOME/.local/bin" >> "$GITHUB_PATH"
          "$HOME/.local/bin/aws" --version
      - name: Configure AWS credentials
        if: github.ref == 'refs/heads/develop'
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Upload APK to S3
        if: github.ref == 'refs/heads/develop'
        working-directory: examples/SampleApp
        # Public download link: https://getstream.io/downloads/rn-sample-app.apk
        run: |
          cp app-build/reactnativesampleapp.apk rn-sample-app.apk
          aws s3 cp rn-sample-app.apk s3://${{ secrets.AWS_S3_BUCKET }} --sse AES256