# Maestro Cloud — E2E template for rebreak-native.
# STATUS: TEMPLATE ONLY — not active. Requires User confirmation before enabling.
#
# Trigger: manual dispatch OR PR to main (commented out — enable after User GO).
# Requires:
#   - MAESTRO_CLOUD_API_KEY in GitHub Actions secrets
#   - EAS_TOKEN in GitHub Actions secrets
#   - E2E_TEST_USER + E2E_TEST_PASSWORD in GitHub Actions secrets
#   - Maestro Cloud account configured at mobile.dev

name: Maestro Cloud E2E (rebreak-native)

on:
  workflow_dispatch:
    inputs:
      platform:
        description: "Target platform"
        required: true
        default: "ios"
        type: choice
        options:
          - ios
          - android
  # Uncomment to run on PRs — only after User approval:
  # pull_request:
  #   branches: [main]
  #   paths:
  #     - "apps/rebreak-native/**"
  #     - "apps/rebreak-native/.maestro/**"

jobs:
  maestro-cloud:
    name: E2E (${{ inputs.platform || 'ios' }})
    runs-on: ubuntu-latest

    # Skip entirely if Maestro Cloud key is not configured —
    # avoids CI failure on forks or before Cloud is set up.
    if: ${{ secrets.MAESTRO_CLOUD_API_KEY != '' }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Install dependencies
        run: pnpm install --frozen-lockfile
        working-directory: apps/rebreak-native

      # Build app via EAS — requires EAS_TOKEN secret and eas.json configured.
      # Profile "preview" must produce a .ipa (iOS) or .apk (Android).
      - name: Build with EAS
        uses: expo/expo-github-action@v8
        with:
          eas-version: latest
          token: ${{ secrets.EAS_TOKEN }}

      - name: EAS Build
        run: |
          eas build \
            --platform ${{ inputs.platform || 'ios' }} \
            --profile preview \
            --non-interactive \
            --output ./build-artifact
        working-directory: apps/rebreak-native

      # Install Maestro CLI
      - name: Install Maestro CLI
        run: curl -Ls "https://get.maestro.mobile.dev" | bash
        env:
          MAESTRO_VERSION: 1.39.0

      - name: Add Maestro to PATH
        run: echo "$HOME/.maestro/bin" >> $GITHUB_PATH

      # Upload build + run flows on Maestro Cloud
      - name: Run Maestro Cloud
        run: |
          maestro cloud \
            --apiKey ${{ secrets.MAESTRO_CLOUD_API_KEY }} \
            --app ./build-artifact \
            --device ${{ inputs.platform || 'ios' }} \
            --env=E2E_TEST_USER=${{ secrets.E2E_TEST_USER }} \
            --env=E2E_TEST_PASSWORD=${{ secrets.E2E_TEST_PASSWORD }} \
            apps/rebreak-native/.maestro/
        working-directory: ${{ github.workspace }}
