workflows:
  ios-workflow:
    name: Flutter iOS Build
    max_build_duration: 60
    instance_type: mac_mini_m1

    environment:
      flutter: stable
      xcode: latest
      cocoapods: default

      ios_signing:
        distribution_type: app_store
        bundle_identifier: fr.geosector.app3

      vars:
        # Bundle ID et nom de l'app
        BUNDLE_ID: "fr.geosector.app3"
        APP_NAME: "GeoSector"

    triggering:
      events:
        - push
      branch_patterns:
        - pattern: main
          include: true
          source: true

    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/Library/Caches/CocoaPods

    scripts:
      - name: Set up Flutter
        script: |
          flutter --version

      - name: Clean and prepare project
        script: |
          flutter clean
          rm -rf ios/Pods
          rm -rf ios/Podfile.lock
          rm -rf ios/.symlinks
          rm -rf ios/Flutter/Flutter.framework
          rm -rf ios/Flutter/Flutter.podspec
          flutter pub get

      - name: Setup iOS dependencies
        script: |
          cd ios
          flutter precache --ios
          pod cache clean --all
          pod repo update
          pod install --repo-update --verbose

      - name: Flutter analyze
        script: |
          flutter analyze

      - name: Build iOS with code signing
        script: |
          # CodeMagic gère automatiquement le code signing via ios_signing
          # Export des variables pour Xcode
          xcode-project use-profiles
          flutter build ipa --release --export-options-plist=/Users/builder/export_options.plist

    artifacts:
      - build/ios/**/*.app
      - build/ios/ipa/*.ipa
      - build/ios/archive/*.xcarchive
      - /tmp/xcodebuild_logs/*.log
      - ios/Pods/Podfile.lock

    publishing:
      email:
        recipients:
          - contact@d6soft.com
        notify:
          success: true
          failure: true

      # App Store Connect
      app_store_connect:
        api_key: $APP_STORE_CONNECT_PRIVATE_KEY
        key_id: $APP_STORE_CONNECT_KEY_IDENTIFIER
        issuer_id: $APP_STORE_CONNECT_ISSUER_ID
        submit_to_testflight: true
        submit_to_app_store: false