#!/usr/bin/env bash
# Start all stopped AKS clusters

set -e

SUBSCRIPTION_ID="fc08d829-4f14-413d-ab27-ce024425db0b"

echo "=== Starting Stopped AKS Clusters ==="
echo ""

# Get stopped clusters
STOPPED=$(az aks list --subscription "$SUBSCRIPTION_ID" --query "[?contains(name, 'az-p-') && powerState.code == 'Stopped'].{name:name, rg:resourceGroup}" -o json)

COUNT=$(echo "$STOPPED" | jq '. | length')

if [ "$COUNT" -eq 0 ]; then
  echo "✅ No stopped clusters found"
  exit 0
fi

echo "Found $COUNT stopped clusters"
echo ""

echo "$STOPPED" | jq -r '.[] | "\(.rg)|\(.name)"' | while IFS='|' read -r rg name; do
  echo "Starting $name in $rg..."
  az aks start --name "$name" --resource-group "$rg" --subscription "$SUBSCRIPTION_ID" --no-wait 2>&1 | grep -v "Warning\|Deprecated" || true
  echo "  ✅ Start initiated"
done

echo ""
echo "=== ✅ Cluster Starts Initiated ==="
echo ""
echo "Clusters are starting in the background."
echo "Wait a few minutes for clusters to start, then re-run Terraform or next steps."
