name: Mobile Bundle Analysis

env:
  RUBY_VERSION: 3.2
  JAVA_VERSION: 17
  WORKSPACE: ${{ github.workspace }}
  APP_PATH: ${{ github.workspace }}/app
  NODE_ENV: "production"

on:
  push:
    branches:
      - dev
      - staging
      - main
    paths:
      - "app/**"
      - "packages/mobile-sdk-alpha/**"
      - ".github/workflows/mobile-bundle-analysis.yml"
      - ".github/actions/**"
  pull_request:
    branches:
      - dev
      - staging
      - main
    paths:
      - "app/**"
      - "packages/mobile-sdk-alpha/**"
      - ".github/workflows/mobile-bundle-analysis.yml"
      - ".github/actions/**"
  workflow_dispatch:

jobs:
  analyze-android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Read and sanitize Node.js version
        shell: bash
        run: |
          if [ ! -f .nvmrc ] || [ -z "$(cat .nvmrc)" ]; then
            echo "❌ .nvmrc is missing or empty"; exit 1;
          fi
          VERSION="$(tr -d '\r\n' < .nvmrc)"
          VERSION="${VERSION#v}"
          if ! [[ "$VERSION" =~ ^[0-9]+(\.[0-9]+){0,2}$ ]]; then
            echo "Invalid .nvmrc content: '$VERSION'"; exit 1;
          fi
          echo "NODE_VERSION=$VERSION" >> "$GITHUB_ENV"
          echo "NODE_VERSION_SANITIZED=${VERSION//\//-}" >> "$GITHUB_ENV"
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - name: Cache Yarn
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            app/node_modules
          cache-version: node-${{ env.NODE_VERSION_SANITIZED }}
      - name: Cache Bundler
        uses: ./.github/actions/cache-bundler
        with:
          path: app/vendor/bundle
          lock-file: app/Gemfile.lock
          cache-version: ruby${{ env.RUBY_VERSION }}
      - name: Cache Gradle
        uses: ./.github/actions/cache-gradle
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
      - name: Generate token for self repositories
        if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false }}
        uses: ./.github/actions/generate-github-token
        id: github-token
        with:
          app-id: ${{ vars.GH_WORKFLOWS_CROSS_ACCESS_ID }}
          private-key: ${{ secrets.GH_WORKFLOWS_CROSS_ACCESS_KEY }}
          configure-netrc: "true"
      - name: Install Mobile Dependencies
        uses: ./.github/actions/mobile-setup
        with:
          app_path: ${{ env.APP_PATH }}
          node_version: ${{ env.NODE_VERSION }}
          ruby_version: ${{ env.RUBY_VERSION }}
          workspace: ${{ env.WORKSPACE }}
        env:
          SELFXYZ_APP_TOKEN: ${{ steps.github-token.outputs.token }}
      - name: Build dependencies
        shell: bash
        run: yarn workspace @selfxyz/common build
      - name: Run Android analysis
        run: yarn analyze:bundle:android
        working-directory: ./app

  analyze-ios:
    if: github.event_name == 'workflow_dispatch' || github.event_name == 'push'
    # runs-on: macos-latest-large
    runs-on: namespace-profile-apple-silicon-6cpu
    steps:
      - uses: actions/checkout@v6
      - name: Read and sanitize Node.js version
        shell: bash
        run: |
          if [ ! -f .nvmrc ] || [ -z "$(cat .nvmrc)" ]; then
            echo "❌ .nvmrc is missing or empty"; exit 1;
          fi
          VERSION="$(tr -d '\r\n' < .nvmrc)"
          VERSION="${VERSION#v}"
          if ! [[ "$VERSION" =~ ^[0-9]+(\.[0-9]+){0,2}$ ]]; then
            echo "Invalid .nvmrc content: '$VERSION'"; exit 1;
          fi
          echo "NODE_VERSION=$VERSION" >> "$GITHUB_ENV"
          echo "NODE_VERSION_SANITIZED=${VERSION//\//-}" >> "$GITHUB_ENV"
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - name: Cache Yarn
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            app/node_modules
          cache-version: node-${{ env.NODE_VERSION_SANITIZED }}
      - name: Cache Bundler
        uses: ./.github/actions/cache-bundler
        with:
          path: app/vendor/bundle
          lock-file: app/Gemfile.lock
          cache-version: ruby${{ env.RUBY_VERSION }}
      - name: Cache CocoaPods
        uses: ./.github/actions/cache-pods
        with:
          path: app/ios/Pods
          lockfile: app/ios/Podfile.lock
      - name: Check Java installation
        run: |
          echo "INSTALL_JAVA=false" >> "$GITHUB_ENV"
          if command -v java &> /dev/null && java -version &> /dev/null; then
            echo "Java already installed: $(java -version 2>&1 | head -n 1)"
          else
            echo "Java not found or not working, will install..."
            echo "INSTALL_JAVA=true" >> "$GITHUB_ENV"
          fi
      - name: Setup Java environment
        if: env.INSTALL_JAVA == 'true'
        uses: actions/setup-java@v4
        with:
          distribution: "temurin"
          java-version: ${{ env.JAVA_VERSION }}
      - name: Generate token for self repositories
        if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false }}
        uses: ./.github/actions/generate-github-token
        id: github-token
        with:
          app-id: ${{ vars.GH_WORKFLOWS_CROSS_ACCESS_ID }}
          private-key: ${{ secrets.GH_WORKFLOWS_CROSS_ACCESS_KEY }}
          configure-netrc: "true"
      - name: Install Mobile Dependencies
        uses: ./.github/actions/mobile-setup
        with:
          app_path: ${{ env.APP_PATH }}
          node_version: ${{ env.NODE_VERSION }}
          ruby_version: ${{ env.RUBY_VERSION }}
          workspace: ${{ env.WORKSPACE }}
        env:
          SELFXYZ_APP_TOKEN: ${{ steps.github-token.outputs.token }}
      - name: Build dependencies
        shell: bash
        run: yarn workspace @selfxyz/common build
      - name: Run iOS analysis
        run: yarn analyze:bundle:ios
        working-directory: ./app
