name: Deploy Sustainability Apps to Production

on:
  workflow_dispatch:

env:
  PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
  REGION: us-central1
  REPO_NAME: sustainability-apps

jobs:
  # ------------------------------------------------------------
  # Build the universal image
  # ------------------------------------------------------------
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      # --- Download WiDS Base Cache (instead of compass cache) ---
      - name: Download WiDS Base Prediction Cache
        uses: dawidd6/action-download-artifact@v6
        with:
          workflow: build_wids_cache.yml
          name: wids-prediction-cache 
          path: .
          search_artifacts: true
          if_no_artifact_found: warn

      # --- Download WiDS Full-Models Cache (instead of compass full models cache) ---
      - name: Download WiDS Full-Models Prediction Cache
        uses: dawidd6/action-download-artifact@v6
        with:
          workflow: build_wids_full_models_cache.yml
          name: wids-prediction-cache-full-models-file
          path: .
          search_artifacts: true
          if_no_artifact_found: warn

      - id: auth
        name: Authenticate to Google Cloud
        uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}

      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v2
        
      - name: Ensure Artifact Registry repo exists
        run: |
          set -euo pipefail
          echo "Ensuring Artifact Registry repository '${REPO_NAME}' exists in '${REGION}'..."
          if gcloud artifacts repositories describe "${REPO_NAME}" --location="${REGION}" --project="${PROJECT_ID}" >/dev/null 2>&1; then
            echo "✅ Artifact Registry repo '${REPO_NAME}' already exists."
          else
            echo "⚠️ Repo '${REPO_NAME}' not found. Creating it..."
            gcloud artifacts repositories create "${REPO_NAME}" \
              --repository-format=docker \
              --location="${REGION}" \
              --project="${PROJECT_ID}" \
              --description="Docker images for sustainability gradio apps"
            echo "✅ Created Artifact Registry repo '${REPO_NAME}'."
          fi
          
      - name: Configure Docker Auth
        run: gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev

      - name: Build and Push Docker Image
        run: |
          IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}"
          echo "Building image: $IMAGE"
          if [ -f "wids_prediction_cache.json.gz" ]; then
            echo "✅ WiDS base cache file found! Will embed into Docker image."
          else
            echo "⚠️ WiDS base cache file NOT found. App may run in slow training mode."
          fi

          if [ -f "wids_prediction_cache_full_models.json.gz" ]; then
            echo "✅ WiDS full-models cache file found! Will embed into Docker image."
          else
            echo "ℹ️ WiDS full-models cache file NOT found."
          fi

          docker build --pull -f Dockerfile_sustainability -t "$IMAGE" .
          docker push "$IMAGE"
          echo "IMAGE=$IMAGE" >> $GITHUB_ENV

  # Language-specific sustainability model building game deployments
  deploy-model-building-game-en-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-en-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-en-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-en-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true
            
  deploy-model-building-game-ca-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-ca-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-ca-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-ca-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true
            
  deploy-model-building-game-es-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-es-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-es-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-es-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true

  # Final variants for EN, ES, CA - Sustainability
  deploy-model-building-game-en-final-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-en-final-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-en-final-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-en-final-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true
            
  deploy-model-building-game-es-final-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-es-final-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-es-final-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-es-final-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true
            
  deploy-model-building-game-ca-final-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy model-building-game-ca-final-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-ca-final-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=4Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=model-building-game-ca-final-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20,DEBUG_LOG=true

  # Sustainability Bias Detective deployments
  deploy-bias-detective-en-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy bias-detective-en-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-en-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=bias-detective-en-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-bias-detective-ca-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy bias-detective-ca-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-ca-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=bias-detective-ca-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-bias-detective-es-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy bias-detective-es-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-es-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=bias-detective-es-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # Sustainability Fairness Fixer deployments
  deploy-fairness-fixer-en-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy fairness-fixer-en-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-en-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=fairness-fixer-en-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-fairness-fixer-ca-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy fairness-fixer-ca-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-ca-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=fairness-fixer-ca-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-fairness-fixer-es-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy fairness-fixer-es-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-es-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=fairness-fixer-es-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # Sustainability Upgrade deployments (renamed from justice-equity-upgrade)
  deploy-sustainability-upgrade-en:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy sustainability-upgrade-en
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: sustainability-upgrade-en
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=sustainability-upgrade-en,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-sustainability-upgrade-ca:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy sustainability-upgrade-ca
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: sustainability-upgrade-ca
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=sustainability-upgrade-ca,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-sustainability-upgrade-es:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy sustainability-upgrade-es
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: sustainability-upgrade-es
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=sustainability-upgrade-es,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # Sustainability Moral Compass Challenge deployments
  deploy-moral-compass-en-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy moral-compass-en-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: moral-compass-en-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=moral-compass-en-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-moral-compass-es-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy moral-compass-es-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: moral-compass-es-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=moral-compass-es-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-moral-compass-ca-sustainability:
    runs-on: ubuntu-latest
    needs: build-and-push
    outputs:
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - uses: google-github-actions/auth@v2
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}
      - uses: google-github-actions/setup-gcloud@v2
      - name: Deploy moral-compass-ca-sustainability
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: moral-compass-ca-sustainability
          image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/gradio-universal:${{ github.sha }}
          region: ${{ env.REGION }}
          flags: >-
            --allow-unauthenticated
            --memory=2Gi
            --cpu=2
            --session-affinity
            --concurrency=20
            --min-instances=1
            --max-instances=150
            --timeout=3000
            --set-env-vars=APP_NAME=moral-compass-ca-sustainability,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # ------------------------------------------------------------
  # Collect and publish all deployed service URLs
  # ------------------------------------------------------------
  collect-urls:
    runs-on: ubuntu-latest
    needs:
      - deploy-model-building-game-en-sustainability
      - deploy-model-building-game-ca-sustainability
      - deploy-model-building-game-es-sustainability
      - deploy-model-building-game-en-final-sustainability
      - deploy-model-building-game-es-final-sustainability
      - deploy-model-building-game-ca-final-sustainability
      - deploy-bias-detective-en-sustainability
      - deploy-bias-detective-ca-sustainability
      - deploy-bias-detective-es-sustainability
      - deploy-fairness-fixer-en-sustainability
      - deploy-fairness-fixer-ca-sustainability
      - deploy-fairness-fixer-es-sustainability
      - deploy-sustainability-upgrade-en
      - deploy-sustainability-upgrade-ca
      - deploy-sustainability-upgrade-es
      - deploy-moral-compass-en-sustainability
      - deploy-moral-compass-es-sustainability
      - deploy-moral-compass-ca-sustainability
    steps:
      - name: Generate JSON of deployed URLs
        run: |
          cat > deployed_urls.json <<'EOF'
          {
            "model-building-game-en-sustainability": "${{ needs.deploy-model-building-game-en-sustainability.outputs.url }}",
            "model-building-game-ca-sustainability": "${{ needs.deploy-model-building-game-ca-sustainability.outputs.url }}",
            "model-building-game-es-sustainability": "${{ needs.deploy-model-building-game-es-sustainability.outputs.url }}",
            "model-building-game-en-final-sustainability": "${{ needs.deploy-model-building-game-en-final-sustainability.outputs.url }}",
            "model-building-game-es-final-sustainability": "${{ needs.deploy-model-building-game-es-final-sustainability.outputs.url }}",
            "model-building-game-ca-final-sustainability": "${{ needs.deploy-model-building-game-ca-final-sustainability.outputs.url }}",
            "bias-detective-en-sustainability": "${{ needs.deploy-bias-detective-en-sustainability.outputs.url }}",
            "bias-detective-ca-sustainability": "${{ needs.deploy-bias-detective-ca-sustainability.outputs.url }}",
            "bias-detective-es-sustainability": "${{ needs.deploy-bias-detective-es-sustainability.outputs.url }}",
            "fairness-fixer-en-sustainability": "${{ needs.deploy-fairness-fixer-en-sustainability.outputs.url }}",
            "fairness-fixer-ca-sustainability": "${{ needs.deploy-fairness-fixer-ca-sustainability.outputs.url }}",
            "fairness-fixer-es-sustainability": "${{ needs.deploy-fairness-fixer-es-sustainability.outputs.url }}",
            "sustainability-upgrade-en": "${{ needs.deploy-sustainability-upgrade-en.outputs.url }}",
            "sustainability-upgrade-ca": "${{ needs.deploy-sustainability-upgrade-ca.outputs.url }}",
            "sustainability-upgrade-es": "${{ needs.deploy-sustainability-upgrade-es.outputs.url }}",
            "moral-compass-en-sustainability": "${{ needs.deploy-moral-compass-en-sustainability.outputs.url }}",
            "moral-compass-es-sustainability": "${{ needs.deploy-moral-compass-es-sustainability.outputs.url }}",
            "moral-compass-ca-sustainability": "${{ needs.deploy-moral-compass-ca-sustainability.outputs.url }}"
          }
          EOF
          echo "Deployed service URLs JSON:"
          cat deployed_urls.json
      - name: Upload deployed_urls.json artifact
        uses: actions/upload-artifact@v4
        with:
          name: deployed-urls
          path: deployed_urls.json
      - name: Append URLs to summary
        run: |
          {
            echo "### Deployed Sustainability App URLs"
            echo ""
            echo '```json'
            cat deployed_urls.json
            echo '```'
          } >> "$GITHUB_STEP_SUMMARY"
