#!/usr/bin/env bash
# Monitor fix progress and continue if needed

set -e

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

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║     FIX PROGRESS MONITOR                                        ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Check current 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 "📊 Current Status:"
echo "   ✅ Ready (Succeeded): $READY"
echo "   ❌ Failed: $FAILED"
echo "   ⚠️  Canceled: $CANCELED"
echo "   🗑️  Deleting: $DELETING"
echo "   ⏳ Creating: $CREATING"
echo ""

# If deletions needed, continue deletion
if [ "$FAILED" -gt 0 ] || [ "$CANCELED" -gt 0 ]; then
  echo "🗑️  Continuing deletion of problematic clusters..."
  cd "$PROJECT_ROOT"
  ./scripts/azure/delete-all-problematic-clusters-parallel.sh 2>&1 | tee -a /tmp/parallel-delete-monitor.log &
  echo "✅ Deletion process started"
  echo ""
fi

# Check if Terraform deployment is running
if [ ! -f /tmp/terraform-apply-redeploy.log ]; then
  if [ "$DELETING" -eq 0 ] && [ "$FAILED" -eq 0 ] && [ "$CANCELED" -eq 0 ]; then
    echo "✅ All deletions complete, starting Terraform deployment..."
    cd "$PROJECT_ROOT"
    ./scripts/azure/wait-and-redeploy.sh 2>&1 | tee /tmp/wait-and-redeploy-monitor.log &
    echo "✅ Terraform deployment started"
  else
    echo "⏳ Waiting for deletions to complete..."
  fi
else
  echo "✅ Terraform deployment already running or completed"
  if tail -5 /tmp/terraform-apply-redeploy.log 2>/dev/null | grep -q "Apply complete"; then
    echo "✅ Terraform deployment completed successfully!"
  fi
fi

echo ""
echo "📝 Monitor logs:"
echo "   tail -f /tmp/parallel-delete*.log"
echo "   tail -f /tmp/wait-and-redeploy*.log"
echo "   tail -f /tmp/terraform-apply-redeploy.log"
echo ""
