#!/usr/bin/env bash
# Wait for deletions to complete, then redeploy with Terraform

set -e

SUBSCRIPTION_ID="fc08d829-4f14-413d-ab27-ce024425db0b"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
TERRAFORM_DIR="$PROJECT_ROOT/terraform/well-architected/cloud-sovereignty"

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║     WAIT FOR DELETIONS & REDEPLOY                              ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

echo "⏳ Waiting for all cluster deletions to complete..."
echo ""

MAX_WAIT=1800  # 30 minutes max
ELAPSED=0
CHECK_INTERVAL=15

while [ $ELAPSED -lt $MAX_WAIT ]; do
  # Count clusters still in deleting or failed/canceled state
  DELETING=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && (provisioningState == 'Deleting' || provisioningState == 'Failed' || provisioningState == 'Canceled')].name" -o tsv 2>/dev/null | wc -l)
  
  if [ "$DELETING" -eq 0 ]; then
    echo ""
    echo "✅ All problematic clusters deleted!"
    break
  fi
  
  MINUTES=$((ELAPSED / 60))
  SECONDS=$((ELAPSED % 60))
  echo "  [${MINUTES}m ${SECONDS}s] Still deleting: $DELETING clusters remaining..."
  
  sleep $CHECK_INTERVAL
  ELAPSED=$((ELAPSED + CHECK_INTERVAL))
done

if [ $ELAPSED -ge $MAX_WAIT ]; then
  echo ""
  echo "⚠️  Timeout waiting for deletions. Proceeding anyway..."
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Step 2: Re-run Terraform Deployment"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

cd "$TERRAFORM_DIR"

echo "Initializing Terraform..."
terraform init -upgrade >/dev/null 2>&1 || true

echo ""
echo "Re-running Terraform deployment..."
echo "This will recreate all deleted clusters with proper configuration"
echo ""
echo "⚠️  This will take 15-30 minutes depending on region availability"
echo ""

# Run Terraform apply with maximum parallelism
terraform apply -parallelism=128 -auto-approve 2>&1 | tee /tmp/terraform-apply-redeploy.log

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Step 3: Verify Deployment"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

echo "Waiting 30 seconds for clusters to stabilize..."
sleep 30

echo ""
echo "Checking cluster status..."
echo ""

READY_COUNT=$(az aks list --subscription "$SUBSCRIPTION_ID" \
  --query "[?contains(name, 'az-p-') && provisioningState == 'Succeeded'].name" -o tsv 2>/dev/null | wc -l)

FAILED_COUNT=$(az aks list --subscription "$SUBSCRIPTION_ID" \
  --query "[?contains(name, 'az-p-') && provisioningState == 'Failed'].name" -o tsv 2>/dev/null | wc -l)

CREATING_COUNT=$(az aks list --subscription "$SUBSCRIPTION_ID" \
  --query "[?contains(name, 'az-p-') && provisioningState == 'Creating'].name" -o tsv 2>/dev/null | wc -l)

echo "📊 Deployment Status:"
echo "   ✅ Ready (Succeeded): $READY_COUNT"
echo "   ❌ Failed: $FAILED_COUNT"
echo "   ⏳ Creating: $CREATING_COUNT"
echo ""

if [ "$CREATING_COUNT" -gt 0 ]; then
  echo "⚠️  Some clusters are still creating. Monitor with:"
  echo "   az aks list --subscription $SUBSCRIPTION_ID --query \"[?contains(name, 'az-p-')].{name:name, state:provisioningState}\" -o table"
fi

if [ "$FAILED_COUNT" -gt 0 ]; then
  echo "⚠️  Some clusters failed. Check logs:"
  echo "   tail -100 /tmp/terraform-apply-redeploy.log"
  echo "   ./scripts/azure/analyze-deployment-failures.sh"
fi

if [ "$READY_COUNT" -ge 20 ]; then
  echo ""
  echo "✅ Deployment successful! Most clusters are ready."
  echo ""
  echo "🎯 Next Steps:"
  echo "   ./scripts/deployment/wait-and-run-all-next-steps.sh"
fi

echo ""
echo "✅ Redeployment process complete!"
echo ""
echo "📝 Logs:"
echo "   • Terraform: /tmp/terraform-apply-redeploy.log"
echo "   • This script: Check output above"
echo ""

