name: Mobile App Build

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:
    inputs:
      build_android_signed:
        description: 'Build signed Android APK/AAB'
        required: true
        type: boolean
        default: false
      build_ios_signed:
        description: 'Build signed iOS IPA'
        required: true
        type: boolean
        default: false
      upload_to_app_store_connect:
        description: 'Upload iOS IPA to App Store Connect'
        required: true
        type: boolean
        default: false

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  setup:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/mobile-app
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: apps/mobile-app/package-lock.json

      - name: Build and distribute shared libraries
        run: |
          cd ../../shared
          chmod +x build-and-distribute.sh
          ./build-and-distribute.sh

      - name: Verify shared library distribution
        run: |
          # Check if files exist and were recently modified
          TARGET_DIRS=(
            "utils/dist/shared/identity-generator"
            "utils/dist/shared/password-generator"
            "utils/dist/shared/models"
            "utils/dist/shared/vault-sql"
          )

          for dir in "${TARGET_DIRS[@]}"; do
            if [ ! -d "$dir" ]; then
              echo "❌ Directory $dir does not exist"
              exit 1
            fi

            # Check if files were modified in the last 5 minutes
            find "$dir" -type f -mmin -5 | grep -q . || {
              echo "❌ Files in $dir were not recently modified"
              exit 1
            }
          done

          echo "✅ Shared library distribution verified"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test

      - name: Run linting
        run: npm run lint

  build-ios:
    needs: setup
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/mobile-app
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: apps/mobile-app/package-lock.json

      - name: Install dependencies
        run: npm ci

      - name: Build JS bundle (iOS - Expo)
        run: |
          mkdir -p build
          npx expo export \
            --dev \
            --output-dir ./build \
            --platform ios

  build-android:
    needs: setup
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Build Android App
        uses: ./.github/actions/build-android-app
        with:
          run_tests: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}

  build-android-signed:
    needs: setup
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.build_android_signed == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Build Android App
        uses: ./.github/actions/build-android-app
        with:
          signed: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}

  build-ios-signed:
    needs: setup
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.build_ios_signed == 'true'
    runs-on: macos-15
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Build iOS App
        uses: ./.github/actions/build-ios-app
        with:
          signed: true
          upload_to_app_store_connect: ${{ github.event.inputs.upload_to_app_store_connect }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ASC_PRIVATE_KEY_BASE64: ${{ secrets.ASC_PRIVATE_KEY_BASE64 }}
          ASC_KEY_ID: ${{ secrets.ASC_KEY_ID }}
          ASC_ISSUER_ID: ${{ secrets.ASC_ISSUER_ID }}
          ASC_TEAM_ID: ${{ secrets.ASC_TEAM_ID }}