name: Deploy Flutter to Firebase (Mac)
on:
  push:
    branches: release

jobs:
  deploy-android:
    runs-on: macos-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Vérifier l'installation Flutter
        run: flutter doctor -v

      - name: Installer les dépendances Flutter
        run: flutter pub get

      - name: Créer les fichiers secrets
        run: |
          echo "${{ secrets.ENV_FILE }}" > .env
          printf '%s' '${{ secrets.FIREBASE_CREDENTIALS }}' > ./android/firebase_credentials.json

      - name: Lancer Fastlane (Force APK)
        working-directory: ./android
        env:
          ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
          RAW_PROPERTIES: ${{ secrets.ANDROID_KEY_PROPERTIES }}
          FIREBASE_ANDROID_APP_ID: ${{ secrets.FIREBASE_ANDROID_APP_ID }}
        run: |
          # 1. Config Ruby
          export PATH="/opt/homebrew/opt/ruby/bin:$PATH"
          export GEM_HOME=$PWD/vendor/bundle
          export GEM_PATH=$PWD/vendor/bundle
          export PATH=$GEM_HOME/bin:$PATH

          # 2. Keystore & Properties
          echo "$ANDROID_KEYSTORE_BASE64" | base64 -D > keystore.jks
          KEYSTORE_PATH=$(pwd)/keystore.jks

          echo "$RAW_PROPERTIES" > temp_props.txt
          STORE_PASS=$(grep "storePassword" temp_props.txt | cut -d'=' -f2 | tr -d '\r')
          KEY_PASS=$(grep "keyPassword" temp_props.txt | cut -d'=' -f2 | tr -d '\r')
          KEY_ALIAS=$(grep "keyAlias" temp_props.txt | cut -d'=' -f2 | tr -d '\r')

          echo "storePassword=$STORE_PASS" > key.properties
          echo "keyPassword=$KEY_PASS" >> key.properties
          echo "keyAlias=$KEY_ALIAS" >> key.properties
          echo "storeFile=$KEYSTORE_PATH" >> key.properties

          # 3. Installation Dépendances
          rm -rf vendor Gemfile.lock .bundle
          echo "source 'https://rubygems.org'" > Gemfile
          echo "gem 'fastlane', '>= 2.210.0'" >> Gemfile
          echo "gem 'fastlane-plugin-firebase_app_distribution'" >> Gemfile
          # Patchs Ruby 3.4
          echo "gem 'abbrev'" >> Gemfile
          echo "gem 'ostruct'" >> Gemfile
          echo "gem 'mutex_m'" >> Gemfile
          echo "gem 'base64'" >> Gemfile
          echo "gem 'csv'" >> Gemfile
          echo "gem 'bigdecimal'" >> Gemfile
          echo "gem 'drb'" >> Gemfile
          echo "gem 'nkf'" >> Gemfile
          
          echo "⬇️ Installation..."
          gem install bundler -v 2.7.2 --force --no-document
          bundle _2.7.2_ update --jobs 4
          
          # 4. CONSTRUCTION ET ENVOI MANUEL EN APK 🗝️
          # On remonte à la racine pour lancer Flutter
          cd ..
          
          echo "🚧 Construction de l'APK (Format compatible sans Play Store)..."
          # C'est cette commande qui change tout : APK au lieu de AppBundle
          flutter build apk --release
          
          # On redescend dans le dossier android pour lancer fastlane
          cd android
          
          echo "🚀 Envoi de l'APK vers Firebase..."
          # On pointe vers le fichier APK généré
          bundle _2.7.2_ exec fastlane run firebase_app_distribution \
            app:"$FIREBASE_ANDROID_APP_ID" \
            android_artifact_path:"../build/app/outputs/flutter-apk/app-release.apk" \
            service_credentials_file:"firebase_credentials.json" \
            release_notes:"Build APK via Act - Test"