#!/bin/bash
# deploy-changed.sh - Deploy only changed services to Kubernetes
# Usage: ./deploy-changed.sh [service-name] or ./deploy-changed.sh --detect

set -e

NAMESPACE="nxtgauge"
KUSTOMIZE_DIR="/Users/ashwin/workspace/nxtgauge-gitops/apps/nxtgauge-backend-rust/base"

# Services mapping (service_name:deployment_name)
declare -A SERVICES=(
  ["gateway"]="nxtgauge-rust-gateway"
  ["users"]="nxtgauge-rust-users"
  ["companies"]="nxtgauge-rust-companies"
  ["job_seekers"]="nxtgauge-rust-job-seekers"
  ["customers"]="nxtgauge-rust-customers"
  ["photographers"]="nxtgauge-rust-photographers"
  ["makeup_artists"]="nxtgauge-rust-makeup-artists"
  ["tutors"]="nxtgauge-rust-tutors"
  ["developers"]="nxtgauge-rust-developers"
  ["video_editors"]="nxtgauge-rust-video-editors"
  ["graphic_designers"]="nxtgauge-rust-graphic-designers"
  ["social_media_managers"]="nxtgauge-rust-social-media-managers"
  ["fitness_trainers"]="nxtgauge-rust-fitness-trainers"
  ["catering_services"]="nxtgauge-rust-catering-services"
  ["ugc_content_creators"]="nxtgauge-rust-ugc-content-creators"
  ["employees"]="nxtgauge-rust-employees"
  ["payments"]="nxtgauge-rust-payments"
  ["cron"]="nxtgauge-cron"
)

deploy_service() {
  local service=$1
  local deployment=${SERVICES[$service]}
  
  if [ -z "$deployment" ]; then
    echo "❌ Unknown service: $service"
    return 1
  fi
  
  echo "🚀 Deploying ${service} (${deployment})..."
  
  # Trigger rolling restart to pick up new image
  kubectl rollout restart deployment/${deployment} -n ${NAMESPACE}
  
  # Wait for rollout to complete
  echo "⏳ Waiting for rollout to complete..."
  kubectl rollout status deployment/${deployment} -n ${NAMESPACE} --timeout=300s
  
  echo "✅ ${service} deployed successfully"
}

# If specific service provided
if [ "$1" != "--detect" ] && [ -n "$1" ]; then
  deploy_service "$1"
  exit 0
fi

# Auto-detect changed services
if [ "$1" == "--detect" ]; then
  echo "🔍 Detecting changed services from git..."
  
  # Get changed files from last commit
  CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || echo "")
  
  if [ -z "$CHANGED_FILES" ]; then
    echo "⚠️  No changes detected"
    exit 0
  fi
  
  # Check if shared crates changed
  SHARED_CHANGED=false
  if echo "$CHANGED_FILES" | grep -q "^crates/"; then
    SHARED_CHANGED=true
    echo "⚠️  Shared crates changed - will redeploy ALL services"
  fi
  
  # Check if K8s manifests changed
  K8S_CHANGED=false
  if echo "$CHANGED_FILES" | grep -q "^apps/nxtgauge-gitops/"; then
    K8S_CHANGED=true
  fi
  
  # Track if any service was deployed
  DEPLOYED=0
  
  # Check each service
  for service in "${!SERVICES[@]}"; do
    # Convert service name to path (replace _ with -)
    service_path=$(echo "$service" | tr '_' '-')
    
    if [ "$SHARED_CHANGED" = true ] || echo "$CHANGED_FILES" | grep -q "^apps/${service_path}/"; then
      deploy_service "$service"
      DEPLOYED=$((DEPLOYED + 1))
    else
      echo "⏭️  ${service} - no changes, skipping"
    fi
  done
  
  if [ $DEPLOYED -eq 0 ]; then
    echo "ℹ️  No services needed deployment"
  else
    echo "🎉 Deployed $DEPLOYED service(s)"
  fi
fi
