---
format_version: "11"
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

project_type: capacitor

app:
  envs:
    - BITRISE_PROJECT_PATH: ""
    - BITRISE_SCHEME: ""

trigger_map:
  - push_branch: main
    workflow: deploy
  - pull_request_source_branch: "*"
    workflow: test
  - tag: "ios-v*"
    workflow: deploy_ios
  - tag: "android-v*"
    workflow: deploy_android

workflows:
  _setup:
    steps:
      - activate-ssh-key@5:
          run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
      - git-clone@9: {}
      - setup-node@1:
          inputs:
            - node_version: "20"
      - script@1:
          title: Install dependencies & build web
          inputs:
            - content: |
                #!/bin/bash
                set -ex
                npm ci
                npm ci --prefix frontend
                npm run build --prefix frontend
      - script@1:
          title: Ensure native platforms & sync Capacitor
          inputs:
            - content: |
                #!/bin/bash
                set -ex
                if [ ! -d "ios/App/App.xcodeproj" ]; then
                  echo "ios/ directory missing from checkout — adding platform"
                  npx cap add ios
                fi
                if [ ! -d "android/app/build.gradle" ]; then
                  echo "android/ directory missing from checkout — adding platform"
                  npx cap add android
                fi
                npx cap sync

  test:
    before_run:
      - _setup
    steps:
      - script@1:
          title: Install Android SDK components
          inputs:
            - content: |
                #!/bin/bash
                set -ex
                yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager \
                  "platforms;android-36" \
                  "build-tools;36.0.0" \
                  "platform-tools" || true
      - android-build@2:
          title: Build Android APK (debug)
          inputs:
            - project_location: android
            - module: app
            - variant: debug

  test_ios:
    before_run:
      - _setup
    steps:
      - cocoapods-install@2:
          inputs:
            - podfile_path: ios/App/Podfile
      - xcode-archive@5:
          title: Build iOS (simulator)
          inputs:
            - project_path: ios/App/App.xcodeproj
            - scheme: App
            - configuration: Debug
            - destination: generic/platform=iOS Simulator
            - output_tool: xcodebuild
            - cache_level: swift

  deploy_ios:
    before_run:
      - _setup
    steps:
      - cocoapods-install@2:
          inputs:
            - podfile_path: ios/App/Podfile
      - certificate-and-profile-installer@1: {}
      - xcode-archive@5:
          title: Archive iOS (release)
          inputs:
            - project_path: ios/App/App.xcodeproj
            - scheme: App
            - configuration: Release
            - destination: generic/platform=iOS
            - output_tool: xcodebuild
            - export_method: app-store
            - cache_level: swift
      - deploy-to-bitrise-io@4: {}

  deploy_android:
    before_run:
      - _setup
    steps:
      - script@1:
          title: Install Android SDK components
          inputs:
            - content: |
                #!/bin/bash
                set -ex
                yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager \
                  "platforms;android-36" \
                  "build-tools;36.0.0" \
                  "platform-tools" || true
      - android-build@2:
          title: Build Android AAB (release)
          inputs:
            - project_location: android
            - module: app
            - variant: release
      - sign-apk@1:
          inputs:
            - use_apk_signer: "true"
      - deploy-to-bitrise-io@4: {}

  deploy:
    before_run:
      - _setup
    steps:
      - script@1:
          title: Install Android SDK components
          inputs:
            - content: |
                #!/bin/bash
                set -ex
                yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager \
                  "platforms;android-36" \
                  "build-tools;36.0.0" \
                  "platform-tools" || true
      - android-build@2:
          title: Build Android AAB (release)
          inputs:
            - project_location: android
            - module: app
            - variant: release
      - sign-apk@1:
          inputs:
            - use_apk_signer: "true"
      - cocoapods-install@2:
          inputs:
            - podfile_path: ios/App/Podfile
      - certificate-and-profile-installer@1: {}
      - xcode-archive@5:
          title: Archive iOS (release)
          inputs:
            - project_path: ios/App/App.xcodeproj
            - scheme: App
            - configuration: Release
            - destination: generic/platform=iOS
            - output_tool: xcodebuild
            - export_method: app-store
            - cache_level: swift
      - deploy-to-bitrise-io@4: {}
