name: Deploy Moral Compass Apps to Production

on:
  workflow_dispatch:

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

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

      # --- NEW STEP: Download the pre-computed cache ---
      - name: Download Prediction Cache
        uses: dawidd6/action-download-artifact@v6
        with:
          workflow: build_model_cache.yml
          name: prediction-cache-file
          path: .
          search_artifacts: true
          if_no_artifact_found: warn

      # NEW: Download the full-models cache built on the complete dataset
      - name: Download Full-Models Prediction Cache
        uses: dawidd6/action-download-artifact@v6
        with:
          workflow: build_full_models_cache.yml
          name: 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: 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 "prediction_cache.json.gz" ]; then
            echo "✅ Base cache file found! Will embed into Docker image."
          else
            echo "⚠️ Base cache file NOT found. App may run in slow training mode."
          fi

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

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

  deploy-judge:
    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 judge
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: judge
          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=judge,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-ai-consequences:
    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 ai-consequences
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: ai-consequences
          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=ai-consequences,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-what-is-ai:
    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 what-is-ai
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: what-is-ai
          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=what-is-ai,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # Language-specific model building game deployments
  deploy-model-building-game-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 model-building-game-en
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-en
          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,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:
    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
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-ca
          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,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:
    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
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-es
          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,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
  deploy-model-building-game-en-final:
    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
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-en-final
          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,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:
    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
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-es-final
          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,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:
    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
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: model-building-game-ca-final
          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,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-ethical-revelation:
    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 ethical-revelation
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: ethical-revelation
          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=ethical-revelation,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-moral-compass-challenge:
    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-challenge
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: moral-compass-challenge
          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-challenge,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20


  # Language-specific Bias Detective variants
  deploy-bias-detective-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 bias-detective-en
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-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=bias-detective-en,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-bias-detective-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 bias-detective-es
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-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=bias-detective-es,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-bias-detective-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 bias-detective-ca
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: bias-detective-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=bias-detective-ca,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20


  # NEW: Language-specific Fairness Fixer variants
  deploy-fairness-fixer-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 fairness-fixer-en
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-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=fairness-fixer-en,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-fairness-fixer-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 fairness-fixer-es
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-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=fairness-fixer-es,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-fairness-fixer-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 fairness-fixer-ca
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: fairness-fixer-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=fairness-fixer-ca,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  # NEW: Language-specific Justice & Equity Upgrade variants
  deploy-justice-equity-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 justice-equity-upgrade-en
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: justice-equity-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=justice-equity-upgrade-en,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-justice-equity-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 justice-equity-upgrade-es
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: justice-equity-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=justice-equity-upgrade-es,GRADIO_SERVER_NAME=0.0.0.0,GRADIO_ANALYTICS_ENABLED=False,GRADIO_NUM_PORTS=1,GRADIO_DEFAULT_CONCURRENCY_LIMIT=20

  deploy-justice-equity-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 justice-equity-upgrade-ca
        id: deploy
        uses: google-github-actions/deploy-cloudrun@v2
        with:
          service: justice-equity-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=justice-equity-upgrade-ca,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-judge
      - deploy-ai-consequences
      - deploy-what-is-ai
      - deploy-model-building-game-en
      - deploy-model-building-game-ca
      - deploy-model-building-game-es
      - deploy-model-building-game-en-final
      - deploy-model-building-game-es-final
      - deploy-model-building-game-ca-final
      - deploy-ethical-revelation
      - deploy-moral-compass-challenge
      - deploy-bias-detective-en
      - deploy-bias-detective-es
      - deploy-bias-detective-ca
      - deploy-fairness-fixer-en
      - deploy-fairness-fixer-es
      - deploy-fairness-fixer-ca
      - deploy-justice-equity-upgrade-en
      - deploy-justice-equity-upgrade-es
      - deploy-justice-equity-upgrade-ca
    steps:
      - name: Generate JSON of deployed URLs
        run: |
          cat > deployed_urls.json <<'EOF'
          {
            "tutorial": "${{ needs.deploy-tutorial.outputs.url }}",
            "judge": "${{ needs.deploy-judge.outputs.url }}",
            "ai-consequences": "${{ needs.deploy-ai-consequences.outputs.url }}",
            "what-is-ai": "${{ needs.deploy-what-is-ai.outputs.url }}",
            "model-building-game-en": "${{ needs.deploy-model-building-game-en.outputs.url }}",
            "model-building-game-ca": "${{ needs.deploy-model-building-game-ca.outputs.url }}",
            "model-building-game-es": "${{ needs.deploy-model-building-game-es.outputs.url }}",
            "model-building-game-en-final": "${{ needs.deploy-model-building-game-en-final.outputs.url }}",
            "model-building-game-es-final": "${{ needs.deploy-model-building-game-es-final.outputs.url }}",
            "model-building-game-ca-final": "${{ needs.deploy-model-building-game-ca-final.outputs.url }}",
            "ethical-revelation": "${{ needs.deploy-ethical-revelation.outputs.url }}",
            "moral-compass-challenge": "${{ needs.deploy-moral-compass-challenge.outputs.url }}",
            "bias-detective-part1": "${{ needs.deploy-bias-detective-part1.outputs.url }}",
            "bias-detective-part2": "${{ needs.deploy-bias-detective-part2.outputs.url }}",
            "bias-detective-en": "${{ needs.deploy-bias-detective-en.outputs.url }}",
            "bias-detective-es": "${{ needs.deploy-bias-detective-es.outputs.url }}",
            "bias-detective-ca": "${{ needs.deploy-bias-detective-ca.outputs.url }}",
            "fairness-fixer": "${{ needs.deploy-fairness-fixer.outputs.url }}",
            "justice-equity-upgrade": "${{ needs.deploy-justice-equity-upgrade.outputs.url }}",
            "fairness-fixer-en": "${{ needs.deploy-fairness-fixer-en.outputs.url }}",
            "fairness-fixer-es": "${{ needs.deploy-fairness-fixer-es.outputs.url }}",
            "fairness-fixer-ca": "${{ needs.deploy-fairness-fixer-ca.outputs.url }}",
            "justice-equity-upgrade-en": "${{ needs.deploy-justice-equity-upgrade-en.outputs.url }}",
            "justice-equity-upgrade-es": "${{ needs.deploy-justice-equity-upgrade-es.outputs.url }}",
            "justice-equity-upgrade-ca": "${{ needs.deploy-justice-equity-upgrade-ca.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 Cloud Run Service URLs"
            echo ""
            echo '```json'
            cat deployed_urls.json
            echo '```'
          } >> "$GITHUB_STEP_SUMMARY"
          } >> "$GITHUB_STEP_SUMMARY"
