name: Build Mobile Apps

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'mobile/**'
      - '.github/workflows/build-mobile.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'mobile/**'
  workflow_dispatch:

jobs:
  build-android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '17'
      
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.4'
          channel: 'stable'
      
      - name: Disable Flutter analytics
        run: flutter config --no-analytics
      
      - name: Install dependencies
        working-directory: mobile
        run: flutter pub get
      
      - name: Generate app icons
        run: |
          pip install Pillow
          python scripts/generate-mobile-icon.py
      - name: Generate launcher icons
        working-directory: mobile
        run: dart run flutter_launcher_icons
      
      - name: Run tests
        working-directory: mobile
        run: flutter test
      
      - name: Build APK
        working-directory: mobile
        env:
          OTEL_EXPORTER_OTLP_ENDPOINT: ${{ secrets.OTEL_EXPORTER_OTLP_ENDPOINT }}
          OTEL_EXPORTER_OTLP_TOKEN: ${{ secrets.OTEL_EXPORTER_OTLP_TOKEN }}
        run: |
          flutter build apk --release \
            --dart-define=OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_ENDPOINT:-}" \
            --dart-define=OTEL_EXPORTER_OTLP_TOKEN="${OTEL_EXPORTER_OTLP_TOKEN:-}"
      
      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: mobile/build/app/outputs/flutter-apk/app-release.apk

  build-ios:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.4'
          channel: 'stable'
      
      - name: Disable Flutter analytics
        run: flutter config --no-analytics
      
      - name: Install dependencies
        working-directory: mobile
        run: flutter pub get
      
      - name: Generate iOS platform files
        working-directory: mobile
        run: flutter create --platforms=ios .
      
      - name: Generate app icons
        run: |
          pip install Pillow
          python scripts/generate-mobile-icon.py
      - name: Generate launcher icons
        working-directory: mobile
        run: |
          dart run flutter_launcher_icons
          dart run flutter_launcher_icons -f flutter_launcher_icons_ios.yaml
      
      # Simulator build avoids Apple Development Team / provisioning (device ipa still needs signing locally).
      # Release mode is not supported for the iOS simulator; use debug (default) or --profile.
      - name: Run tests
        working-directory: mobile
        run: flutter test
      
      - name: Build iOS (simulator)
        working-directory: mobile
        env:
          OTEL_EXPORTER_OTLP_ENDPOINT: ${{ secrets.OTEL_EXPORTER_OTLP_ENDPOINT }}
          OTEL_EXPORTER_OTLP_TOKEN: ${{ secrets.OTEL_EXPORTER_OTLP_TOKEN }}
        run: |
          flutter build ios --simulator \
            --dart-define=OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_ENDPOINT:-}" \
            --dart-define=OTEL_EXPORTER_OTLP_TOKEN="${OTEL_EXPORTER_OTLP_TOKEN:-}"
      
      - name: Create iOS archive
        if: success()
        working-directory: mobile
        run: |
          mkdir -p dist
          if [ -d "build/ios/iphonesimulator/Runner.app" ]; then
            cd build/ios/iphonesimulator
            zip -r ../../../dist/TimeTracker-iOS.zip Runner.app
            cd ../../..
          else
            echo "Warning: Runner.app not found at build/ios/iphonesimulator/Runner.app"
            ls -la build/ios/ || true
            ls -la build/ || true
          fi
      
      - name: Upload iOS build
        if: success()
        uses: actions/upload-artifact@v4
        with:
          name: ios-build
          path: mobile/dist/*.zip
          if-no-files-found: warn
