workflows:
  android-release:
    name: Android Release Build
    max_build_duration: 60
    instance_type: mac_mini_m1
    
    environment:
      groups:
        - google_play_credentials  # Add in Codemagic UI
        - android_keystore         # Add in Codemagic UI
        - firebase_config          # Add in Codemagic UI - contains GOOGLE_SERVICES_JSON (base64 encoded)
      node: 18.17.0
      java: 17
      vars:
        PACKAGE_NAME: "com.qtvoice.softphone"
        GOOGLE_PLAY_TRACK: internal  # internal, alpha, beta, production
    
    triggering:
      events:
        - push
        - tag
      branch_patterns:
        - pattern: 'main'
          include: true
        - pattern: 'release/*'
          include: true
      tag_patterns:
        - pattern: 'v*'
          include: true
    
    scripts:
      # Set up environment
      - name: Set up local.properties
        script: |
          echo "sdk.dir=$ANDROID_SDK_ROOT" > android/local.properties
      
      # Install Node dependencies
      - name: Install Node dependencies
        script: |
          npm install
      
      # Build Next.js app
      - name: Build Next.js
        script: |
          npm run build
          npx next export -o out || true
      
      # Set up Android keystore
      - name: Set up Android keystore
        script: |
          echo $CM_KEYSTORE | base64 --decode > android/app/keystore.jks
          cat >> android/app/build.gradle << EOF
          android {
              signingConfigs {
                  release {
                      storeFile file('keystore.jks')
                      storePassword System.getenv('CM_KEYSTORE_PASSWORD')
                      keyAlias System.getenv('CM_KEY_ALIAS')
                      keyPassword System.getenv('CM_KEY_PASSWORD')
                  }
              }
              buildTypes {
                  release {
                      signingConfig signingConfigs.release
                  }
              }
          }
          EOF
      
      # Set up Firebase
      - name: Set up Firebase config
        script: |
          echo $GOOGLE_SERVICES_JSON | base64 --decode > android/app/google-services.json
      
      # Build Android AAB (for Play Store)
      - name: Build Android App Bundle
        script: |
          cd android
          ./gradlew bundleRelease
    
    artifacts:
      - android/app/build/outputs/**/*.aab
      - android/app/build/outputs/**/*.apk
      - android/app/build/outputs/**/mapping.txt
    
    publishing:
      google_play:
        credentials: $GCLOUD_SERVICE_ACCOUNT_CREDENTIALS
        track: $GOOGLE_PLAY_TRACK
        submit_as_draft: true

  android-debug:
    name: Android Debug Build
    max_build_duration: 45
    instance_type: mac_mini_m1
    
    environment:
      groups:
        - firebase_config  # Add in Codemagic UI - contains GOOGLE_SERVICES_JSON (base64 encoded)
      node: 18.17.0
      java: 17
    
    triggering:
      events:
        - pull_request
      branch_patterns:
        - pattern: '*'
          include: true
    
    scripts:
      - name: Set up local.properties
        script: |
          echo "sdk.dir=$ANDROID_SDK_ROOT" > android/local.properties
      
      - name: Install Node dependencies
        script: |
          npm install
      
      - name: Build Next.js
        script: |
          npm run build
      
      # Set up Firebase for push notifications
      - name: Set up Firebase config
        script: |
          echo $GOOGLE_SERVICES_JSON | base64 --decode > android/app/google-services.json
      
      - name: Build Android Debug APK
        script: |
          cd android
          ./gradlew assembleDebug
    
    artifacts:
      - android/app/build/outputs/**/*.apk
