#!/bin/bash

# Configuration
PROJECT_ID="mt-api-gateway-390018"
SERVICE_NAME="projects-cloudrun"
REGION="us-central1"
IMAGE_URL="gcr.io/$PROJECT_ID/$SERVICE_NAME:latest"

echo "--------------------------------------------------------"
echo "Step 1: Building and Pushing image via Cloud Build..."
echo "--------------------------------------------------------"

# This command builds the image and pushes it to GCR automatically
if gcloud builds submit --tag $IMAGE_URL .; then
    
    echo "--------------------------------------------------------"
    echo "Step 2: Deploying to Cloud Run..."
    echo "--------------------------------------------------------"
    
    gcloud run deploy $SERVICE_NAME \
      --image=$IMAGE_URL \
      --timeout=10m \
      --region=$REGION \
      --platform=managed \
      --allow-unauthenticated

    echo "--------------------------------------------------------"
    echo "Success! Your changes are live."
    echo "--------------------------------------------------------"
else
    echo "--------------------------------------------------------"
    echo "ERROR: Build failed. Check the logs above."
    echo "--------------------------------------------------------"
    exit 1
fi