#!/bin/bash

# Deploy NavFlow Proxy Server to Google Cloud Run

set -e

PROJECT_ID=${1:-"buildship-pfw15o"}
SERVICE_NAME="navflow-proxy"
REGION="us-central1"

echo "🚀 Deploying NavFlow Proxy Server to Google Cloud Run"
echo "📋 Project: $PROJECT_ID"
echo "🌍 Region: $REGION"
echo "🔧 Service: $SERVICE_NAME"
echo ""

# Build and deploy using Cloud Build
echo "📦 Building and deploying with Cloud Build..."
gcloud builds submit --config cloudbuild.yaml --project $PROJECT_ID

# Get the service URL
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region $REGION --project $PROJECT_ID --format "value(status.url)")

echo ""
echo "✅ Deployment completed successfully!"
echo ""
echo "🌐 Service URL: $SERVICE_URL"
echo "🔗 WebSocket endpoint: ${SERVICE_URL/https:/wss:}/tunnel"
echo "💓 Health check: $SERVICE_URL/health"
echo "📊 Stats: $SERVICE_URL/stats"
echo ""
echo "🔧 To use with local browser server:"
echo "   export PROXY_SERVER_URL=\"${SERVICE_URL/https:/wss:}/tunnel\""
echo "   npm run dev:browser-server"
echo ""
echo "🎯 Frontend configuration:"
echo "   - Server URL: [tunnel will provide subdomain]"
echo "   - Password: [tunnel will provide password]"