# Reusable script for building the Flutter app
client-app-android-apk-build-script: &client-app-android-apk-build-script
  name: 👷🤖 Build Client App APK (Android)
  script: |
    make -C ../.. mobile-client-build ENV=$ENV PLATFORM=android BUILD_TYPE=apk

client-app-ios-build-script: &client-app-ios-build-script
  name: 👷🍎 Build Client App (iOS)
  script: |
    make -C ../.. mobile-client-build ENV=$ENV PLATFORM=ios

staff-app-android-apk-build-script: &staff-app-android-apk-build-script
  name: 👷🤖 Build Staff App APK (Android)
  script: |
    make -C ../.. mobile-staff-build ENV=$ENV PLATFORM=android BUILD_TYPE=apk

staff-app-ios-build-script: &staff-app-ios-build-script
  name: 👷🍎 Build Staff App (iOS)
  script: |
    make -C ../.. mobile-staff-build ENV=$ENV PLATFORM=ios

# Reusable script for distributing Android to Firebase
distribute-android-script: &distribute-android-script
  name: 🚛🤖 Distribute Android to Firebase App Distribution
  script: |
    # Distribute Android APK
    firebase appdistribution:distribute "build/app/outputs/flutter-apk/app-${ENV}-release.apk" \
      --app $FIREBASE_APP_ID_ANDROID \
      --release-notes "Build $FCI_BUILD_NUMBER - Environment: $ENV" \
      --groups "$FIREBASE_TESTER_GROUPS" \
      --token $FIREBASE_TOKEN

# Reusable script for distributing iOS to Firebase
distribute-ios-script: &distribute-ios-script
  name: 🚛🍎 Distribute iOS to Firebase App Distribution
  script: |
    # Distribute iOS
    firebase appdistribution:distribute "build/ios/ipa/app.ipa" \
      --app $FIREBASE_APP_ID_IOS \
      --release-notes "Build $FCI_BUILD_NUMBER - Environment: $ENV" \
      --groups "$FIREBASE_TESTER_GROUPS" \
      --token $FIREBASE_TOKEN

workflows:
  # =================================================================================
  # Base workflow for client_app
  # =================================================================================
  client-app-base: &client-app-base
    name: Client App Base
    working_directory: mobile-apps/client-app
    instance_type: mac_mini_m2
    max_build_duration: 60
    environment:
      flutter: stable
      xcode: latest
      cocoapods: default
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $FCI_BUILD_DIR/mobile-apps/client-app/build
        - $FCI_BUILD_DIR/mobile-apps/client-app/.dart_tool

  # =================================================================================
  # Base workflow for staff_app
  # =================================================================================
  staff-app-base: &staff-app-base
    name: Staff App Base
    working_directory: mobile-apps/staff-app
    instance_type: mac_mini_m2
    max_build_duration: 60
    environment:
      flutter: stable
      xcode: latest
      cocoapods: default
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $FCI_BUILD_DIR/mobile-apps/staff-app/build
        - $FCI_BUILD_DIR/mobile-apps/staff-app/.dart_tool

  # =================================================================================
  # Client App Workflows - Android
  # =================================================================================
  client-app-dev-android:
    <<: *client-app-base
    name: 🚛🤖 Client App Dev (Android App Distribution)
    environment:
      flutter: stable
      xcode: latest
      cocoapods: default
      groups:
        - client_app_dev_credentials
      vars:
        ENV: dev
    scripts:
      - *client-app-android-apk-build-script
      - *distribute-android-script

  client-app-staging-android:
    <<: *client-app-base
    name: 🚛🤖 Client App Staging (Android App Distribution)
    environment:
      flutter: stable
      xcode: latest
      cocoapods: default
      groups:
        - client_app_staging_credentials
      vars:
        ENV: staging
    scripts:
      - *client-app-android-apk-build-script
      - *distribute-android-script

  client-app-prod-android:
    <<: *client-app-base
    name: 🚛🤖 Client App Prod (Android App Distribution)
    environment:
      groups:
        - client_app_prod_credentials
      vars:
        ENV: prod
    scripts:
      - *client-app-android-apk-build-script
      - *distribute-android-script

  # =================================================================================
  # Client App Workflows - iOS
  # =================================================================================
  client-app-dev-ios:
    <<: *client-app-base
    name: 🚛🍎 Client App Dev (iOS App Distribution)
    environment:
      groups:
        - client_app_dev_credentials
      vars:
        ENV: dev
    scripts:
      - *client-app-ios-build-script
      - *distribute-ios-script

  client-app-staging-ios:
    <<: *client-app-base
    name: 🚛🍎 Client App Staging (iOS App Distribution)
    environment:
      groups:
        - client_app_staging_credentials
      vars:
        ENV: staging
    scripts:
      - *client-app-ios-build-script
      - *distribute-ios-script

  client-app-prod-ios:
    <<: *client-app-base
    name: 🚛🍎 Client App Prod (iOS App Distribution)
    environment:
      groups:
        - client_app_prod_credentials
      vars:
        ENV: prod
    scripts:
      - *client-app-ios-build-script
      - *distribute-ios-script

  # =================================================================================
  # Staff App Workflows - Android
  # =================================================================================
  staff-app-dev-android:
    <<: *staff-app-base
    name: 🚛🤖👨‍🍳 Staff App Dev (Android App Distribution)
    environment:
      groups:
        - staff_app_dev_credentials
      vars:
        ENV: dev
    scripts:
      - *staff-app-android-apk-build-script
      - *distribute-android-script

  staff-app-staging-android:
    <<: *staff-app-base
    name: 🚛🤖👨‍🍳 Staff App Staging (Android App Distribution)
    environment:
      groups:
        - staff_app_staging_credentials
      vars:
        ENV: staging
    scripts:
      - *staff-app-android-apk-build-script
      - *distribute-android-script

  staff-app-prod-android:
    <<: *staff-app-base
    name: 🚛🤖👨‍🍳 Staff App Prod (Android App Distribution)
    environment:
      groups:
        - staff_app_prod_credentials
      vars:
        ENV: prod
    scripts:
      - *staff-app-android-apk-build-script
      - *distribute-android-script

  # =================================================================================
  # Staff App Workflows - iOS
  # =================================================================================
  staff-app-dev-ios:
    <<: *staff-app-base
    name: 🚛🍎👨‍🍳 Staff App Dev (iOS App Distribution)
    environment:
      groups:
        - staff_app_dev_credentials
      vars:
        ENV: dev
    scripts:
      - *staff-app-ios-build-script
      - *distribute-ios-script

  staff-app-staging-ios:
    <<: *staff-app-base
    name: 🚛🍎👨‍🍳 Staff App Staging (iOS App Distribution)
    environment:
      groups:
        - staff_app_staging_credentials
      vars:
        ENV: staging
    scripts:
      - *staff-app-ios-build-script
      - *distribute-ios-script

  staff-app-prod-ios:
    <<: *staff-app-base
    name: 🚛🍎👨‍🍳 Staff App Prod (iOS App Distribution)
    environment:
      groups:
        - staff_app_prod_credentials
      vars:
        ENV: prod
    scripts:
      - *staff-app-ios-build-script
      - *distribute-ios-script
      