# =============================================================================
# Cloud Build: HTTP Services (Whisper & FFmpeg)
# =============================================================================
# Builds and deploys the autoscaling HTTP services to Cloud Run.
#
# Usage:
#   gcloud builds submit --config=infra/cloud-run/cloudbuild-http-services.yaml .
#
# Or trigger automatically on push to main branch via Cloud Build triggers.
# =============================================================================

steps:
  # =========================================================================
  # Build Docker Images
  # =========================================================================

  # Build Whisper HTTP Service image
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '-f'
      - 'backend/Dockerfile.whisper-service'
      - '-t'
      - 'gcr.io/$PROJECT_ID/whisper-http-service:$COMMIT_SHA'
      - '-t'
      - 'gcr.io/$PROJECT_ID/whisper-http-service:latest'
      - 'backend/'
    id: 'build-whisper-service'

  # Build FFmpeg HTTP Service image
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '-f'
      - 'backend/Dockerfile.ffmpeg-service'
      - '-t'
      - 'gcr.io/$PROJECT_ID/ffmpeg-http-service:$COMMIT_SHA'
      - '-t'
      - 'gcr.io/$PROJECT_ID/ffmpeg-http-service:latest'
      - 'backend/'
    id: 'build-ffmpeg-service'

  # =========================================================================
  # Push Images to Container Registry
  # =========================================================================

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/whisper-http-service:$COMMIT_SHA']
    id: 'push-whisper-service-sha'
    waitFor: ['build-whisper-service']

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/whisper-http-service:latest']
    id: 'push-whisper-service-latest'
    waitFor: ['build-whisper-service']

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/ffmpeg-http-service:$COMMIT_SHA']
    id: 'push-ffmpeg-service-sha'
    waitFor: ['build-ffmpeg-service']

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/ffmpeg-http-service:latest']
    id: 'push-ffmpeg-service-latest'
    waitFor: ['build-ffmpeg-service']

  # =========================================================================
  # Deploy to Cloud Run
  # =========================================================================

  # Deploy Whisper HTTP Service
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'gcloud'
    args:
      - 'run'
      - 'deploy'
      - 'whisper-http-service'
      - '--image=gcr.io/$PROJECT_ID/whisper-http-service:$COMMIT_SHA'
      - '--region=$_REGION'
      - '--platform=managed'
      - '--no-allow-unauthenticated'  # Internal only
      - '--port=8080'
      - '--memory=32Gi'
      - '--cpu=8'
      - '--min-instances=0'
      - '--max-instances=10'
      - '--concurrency=1'
      - '--timeout=300'
      - '--execution-environment=gen2'
      - '--no-cpu-throttling'
      - '--cpu-boost'
      - '--set-env-vars=APP_ENV=prod,PYTHONPATH=/app,WHISPER_MODEL=medium,GCS_BUCKET=$_GCS_BUCKET'
      - '--service-account=accessible-video-worker@$PROJECT_ID.iam.gserviceaccount.com'
    id: 'deploy-whisper-service'
    waitFor: ['push-whisper-service-sha']

  # Deploy FFmpeg HTTP Service
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'gcloud'
    args:
      - 'run'
      - 'deploy'
      - 'ffmpeg-http-service'
      - '--image=gcr.io/$PROJECT_ID/ffmpeg-http-service:$COMMIT_SHA'
      - '--region=$_REGION'
      - '--platform=managed'
      - '--no-allow-unauthenticated'  # Internal only
      - '--port=8080'
      - '--memory=32Gi'
      - '--cpu=8'
      - '--min-instances=0'
      - '--max-instances=20'
      - '--concurrency=1'
      - '--timeout=600'
      - '--execution-environment=gen2'
      - '--no-cpu-throttling'
      - '--cpu-boost'
      - '--set-env-vars=APP_ENV=prod,PYTHONPATH=/app,GCS_BUCKET=$_GCS_BUCKET'
      - '--service-account=accessible-video-worker@$PROJECT_ID.iam.gserviceaccount.com'
    id: 'deploy-ffmpeg-service'
    waitFor: ['push-ffmpeg-service-sha']

  # =========================================================================
  # Output Service URLs
  # =========================================================================

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        echo "=============================================="
        echo "HTTP Services Deployed Successfully!"
        echo "=============================================="
        echo ""
        echo "Whisper Service:"
        gcloud run services describe whisper-http-service --region=$_REGION --format='value(status.url)'
        echo ""
        echo "FFmpeg Service:"
        gcloud run services describe ffmpeg-http-service --region=$_REGION --format='value(status.url)'
        echo ""
        echo "=============================================="
        echo "To enable Cloud Run usage, set these environment variables:"
        echo "  WHISPER_SERVICE_URL=<whisper-service-url>"
        echo "  FFMPEG_SERVICE_URL=<ffmpeg-service-url>"
        echo "=============================================="
    id: 'output-urls'
    waitFor: ['deploy-whisper-service', 'deploy-ffmpeg-service']

substitutions:
  _REGION: 'us-central1'
  _GCS_BUCKET: 'accessible-video'

options:
  machineType: 'E2_HIGHCPU_8'
  diskSizeGb: '100'
  dynamic_substitutions: true

timeout: '2400s'  # 40 minutes (Whisper image build takes time due to model download)

# Images to be pushed (for Cloud Build to track)
images:
  - 'gcr.io/$PROJECT_ID/whisper-http-service:$COMMIT_SHA'
  - 'gcr.io/$PROJECT_ID/whisper-http-service:latest'
  - 'gcr.io/$PROJECT_ID/ffmpeg-http-service:$COMMIT_SHA'
  - 'gcr.io/$PROJECT_ID/ffmpeg-http-service:latest'
