steps:
  # Build the Docker image
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-f', 'backend/Dockerfile', '--target', 'production', '-t', 'gcr.io/$PROJECT_ID/accessible-video-api:$COMMIT_SHA', 'backend/']
    id: 'build-api'

  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-f', 'backend/Dockerfile', '--target', 'worker', '-t', 'gcr.io/$PROJECT_ID/accessible-video-worker:$COMMIT_SHA', 'backend/']
    id: 'build-worker'

  # Push the images to Container Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/accessible-video-api:$COMMIT_SHA']
    id: 'push-api'
    waitFor: ['build-api']

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/accessible-video-worker:$COMMIT_SHA']
    id: 'push-worker'
    waitFor: ['build-worker']

  # Deploy API service to Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'gcloud'
    args:
      - 'run'
      - 'deploy'
      - 'accessible-video-api'
      - '--image=gcr.io/$PROJECT_ID/accessible-video-api:$COMMIT_SHA'
      - '--region=$_REGION'
      - '--platform=managed'
      - '--allow-unauthenticated'
      - '--port=8000'
      - '--memory=2Gi'
      - '--cpu=2'
      - '--min-instances=1'
      - '--max-instances=10'
      - '--concurrency=100'
      - '--timeout=3600'
      - '--set-env-vars=APP_ENV=prod,PYTHONPATH=/app'
      - '--set-secrets=MONGODB_URL=projects/$PROJECT_ID/secrets/mongodb-url:latest'
      - '--set-secrets=REDIS_URL=projects/$PROJECT_ID/secrets/redis-url:latest'
      - '--set-secrets=JWT_SECRET_KEY=projects/$PROJECT_ID/secrets/jwt-secret:latest'
      - '--set-secrets=GEMINI_API_KEY=projects/$PROJECT_ID/secrets/gemini-api-key:latest'
      - '--set-secrets=SENDGRID_API_KEY=projects/$PROJECT_ID/secrets/sendgrid-api-key:latest'
      - '--set-secrets=ELEVENLABS_API_KEY=projects/$PROJECT_ID/secrets/elevenlabs-api-key:latest'
      - '--service-account=accessible-video-api@$PROJECT_ID.iam.gserviceaccount.com'
    id: 'deploy-api'
    waitFor: ['push-api']

  # Deploy Worker service to Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'gcloud'
    args:
      - 'run'
      - 'deploy'
      - 'accessible-video-worker'
      - '--image=gcr.io/$PROJECT_ID/accessible-video-worker:$COMMIT_SHA'
      - '--region=$_REGION'
      - '--platform=managed'
      - '--no-allow-unauthenticated'
      - '--memory=4Gi'
      - '--cpu=4'
      - '--min-instances=0'
      - '--max-instances=5'
      - '--concurrency=1'
      - '--timeout=3600'
      - '--set-env-vars=APP_ENV=prod,PYTHONPATH=/app,C_FORCE_ROOT=1'
      - '--set-secrets=MONGODB_URL=projects/$PROJECT_ID/secrets/mongodb-url:latest'
      - '--set-secrets=REDIS_URL=projects/$PROJECT_ID/secrets/redis-url:latest'
      - '--set-secrets=GEMINI_API_KEY=projects/$PROJECT_ID/secrets/gemini-api-key:latest'
      - '--set-secrets=ELEVENLABS_API_KEY=projects/$PROJECT_ID/secrets/elevenlabs-api-key:latest'
      - '--service-account=accessible-video-worker@$PROJECT_ID.iam.gserviceaccount.com'
    id: 'deploy-worker'
    waitFor: ['push-worker']

substitutions:
  _REGION: 'us-central1'

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

timeout: '1800s'