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-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      # Existing base 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: 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

  # Parallel deploy jobs unchanged...