# Every merge to main deploys to the sandbox app (docs/release/environments.md).
#
# Backend first, then client. The fingerprint decides the client path:
#   unchanged → OTA update on the `sandbox` channel (testers get it on next launch)
#   changed   → store builds for both platforms → TestFlight + Play internal track
#
# Requires in the `preview` EAS environment: CONVEX_DEPLOY_KEY (sandbox project),
# GLIDE_APP_VARIANT=sandbox, and the EXPO_PUBLIC_* client vars.
name: Deploy to sandbox

# DISABLED — manual-only for now. Sandbox deploys are run by hand while getting
# a feel for the OTA-vs-build-vs-submit cadence (see the manual runbook in
# docs/release/environments.md). To automate, restore the push trigger below and
# connect the repo to EAS's GitHub integration.
on:
  workflow_dispatch: {}
  # push:
  #   branches: ['main']

jobs:
  deploy_backend:
    name: Deploy Convex (sandbox)
    environment: preview
    steps:
      - uses: eas/checkout
      - uses: eas/install_node_modules
      - name: Deploy to the sandbox Convex project
        run: bunx convex deploy

  fingerprint:
    name: Compute fingerprint
    type: fingerprint
    environment: preview

  get_ios_build:
    name: Find existing iOS build
    needs: [fingerprint]
    type: get-build
    params:
      platform: ios
      profile: sandbox
      fingerprint_hash: ${{ needs.fingerprint.outputs.ios_fingerprint_hash }}

  get_android_build:
    name: Find existing Android build
    needs: [fingerprint]
    type: get-build
    params:
      platform: android
      profile: sandbox
      fingerprint_hash: ${{ needs.fingerprint.outputs.android_fingerprint_hash }}

  publish_update:
    name: OTA update (sandbox channel)
    needs: [deploy_backend, get_ios_build, get_android_build]
    if: ${{ needs.get_ios_build.outputs.build_id && needs.get_android_build.outputs.build_id }}
    type: update
    environment: preview
    params:
      channel: sandbox

  build_ios:
    name: Build iOS (sandbox)
    needs: [deploy_backend, get_ios_build]
    if: ${{ !needs.get_ios_build.outputs.build_id }}
    type: build
    params:
      platform: ios
      profile: sandbox

  submit_ios:
    name: Submit to TestFlight (internal)
    needs: [build_ios]
    type: submit
    params:
      build_id: ${{ needs.build_ios.outputs.build_id }}
      profile: sandbox

  build_android:
    name: Build Android (sandbox)
    needs: [deploy_backend, get_android_build]
    if: ${{ !needs.get_android_build.outputs.build_id }}
    type: build
    params:
      platform: android
      profile: sandbox

  submit_android:
    name: Submit to Play internal track
    needs: [build_android]
    type: submit
    params:
      build_id: ${{ needs.build_android.outputs.build_id }}
      profile: sandbox
