#!/usr/bin/env bash
# Continuously check status and continue with next steps

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 "=== Continuous Status Check & Continue ==="
echo ""

while true; do
  # Check status
  FAILED=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Failed'].name" -o tsv 2>/dev/null | wc -l)
  
  CANCELED=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Canceled'].name" -o tsv 2>/dev/null | wc -l)
  
  DELETING=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Deleting'].name" -o tsv 2>/dev/null | wc -l)
  
  READY=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Succeeded'].name" -o tsv 2>/dev/null | wc -l)
  
  CREATING=$(az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Creating'].name" -o tsv 2>/dev/null | wc -l)
  
  echo "$(date '+%Y-%m-%d %H:%M:%S') - Status: Ready=$READY, Failed=$FAILED, Canceled=$CANCELED, Deleting=$DELETING, Creating=$CREATING"
  
  # If deletions complete and Terraform not started, start it
  if [ "$DELETING" -eq 0 ] && [ "$FAILED" -eq 0 ] && [ "$CANCELED" -eq 0 ]; then
    if [ ! -f /tmp/terraform-apply-redeploy.log ] || ! ps aux | grep -q "[t]erraform apply"; then
      echo "✅ All deletions complete! Starting Terraform deployment..."
      cd "$TERRAFORM_DIR"
      terraform init -upgrade >/dev/null 2>&1 || true
      terraform apply -parallelism=128 -auto-approve 2>&1 | tee /tmp/terraform-apply-redeploy.log &
      echo "✅ Terraform deployment started"
      break
    fi
  fi
  
  # If Terraform complete and clusters ready, run next steps
  if [ -f /tmp/terraform-apply-redeploy.log ] && tail -10 /tmp/terraform-apply-redeploy.log | grep -q "Apply complete"; then
    if [ "$READY" -ge 20 ]; then
      echo "✅ Terraform deployment complete with $READY ready clusters!"
      echo "Starting next steps..."
      cd "$PROJECT_ROOT"
      ./scripts/deployment/wait-and-run-all-next-steps.sh 2>&1 | tee /tmp/next-steps-after-fix.log &
      echo "✅ Next steps started"
      break
    fi
  fi
  
  # Check if still processing
  if [ "$DELETING" -gt 0 ] || [ "$CREATING" -gt 0 ]; then
    echo "  ⏳ Still processing... (checking again in 30 seconds)"
    sleep 30
  else
    echo "  ⏸️  Waiting... (checking again in 60 seconds)"
    sleep 60
  fi
done

echo ""
echo "✅ Monitoring complete"
