#!/usr/bin/env bash
# Continue fix if script didn't complete all steps

set -e

SUBSCRIPTION_ID="fc08d829-4f14-413d-ab27-ce024425db0b"
TERRAFORM_DIR="$HOME/projects/smom-dbis-138/terraform/well-architected/cloud-sovereignty"

echo "=== Continuing Fix Process ==="
echo ""

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

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

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

echo "Current Status:"
echo "  Failed clusters: $FAILED_COUNT"
echo "  Canceled clusters: $CANCELED_COUNT"
echo "  Deleting clusters: $DELETING_COUNT"
echo ""

if [ "$DELETING_COUNT" -gt 0 ]; then
  echo "⏳ Waiting for deletions to complete..."
  while [ "$DELETING_COUNT" -gt 0 ]; do
    sleep 10
    DELETING_COUNT=$(az aks list --subscription "$SUBSCRIPTION_ID" \
      --query "[?contains(name, 'az-p-') && provisioningState == 'Deleting'].name" -o tsv 2>/dev/null | wc -l)
    echo "  Still deleting: $DELETING_COUNT clusters..."
  done
  echo "✅ All deletions complete"
  echo ""
fi

# Delete remaining failed clusters
if [ "$FAILED_COUNT" -gt 0 ]; then
  echo "Deleting remaining failed clusters..."
  az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Failed'].{name:name, rg:resourceGroup}" -o json | \
    jq -r '.[] | "\(.rg)|\(.name)"' | while IFS='|' read -r rg name; do
      echo "Deleting: $name"
      az aks delete --resource-group "$rg" --name "$name" --subscription "$SUBSCRIPTION_ID" --yes --no-wait 2>&1 | grep -v "^$" || true
    done
  echo "✅ Deletion initiated for failed clusters"
  echo ""
fi

# Delete remaining canceled clusters
if [ "$CANCELED_COUNT" -gt 0 ]; then
  echo "Deleting remaining canceled clusters..."
  az aks list --subscription "$SUBSCRIPTION_ID" \
    --query "[?contains(name, 'az-p-') && provisioningState == 'Canceled'].{name:name, rg:resourceGroup}" -o json | \
    jq -r '.[] | "\(.rg)|\(.name)"' | while IFS='|' read -r rg name; do
      echo "Deleting: $name"
      az aks delete --resource-group "$rg" --name "$name" --subscription "$SUBSCRIPTION_ID" --yes --no-wait 2>&1 | grep -v "^$" || true
    done
  echo "✅ Deletion initiated for canceled clusters"
  echo ""
fi

echo "Waiting 60 seconds for deletions to process..."
sleep 60

echo ""
echo "=== Re-running Terraform Deployment ==="
echo ""

cd "$TERRAFORM_DIR"

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

echo ""
echo "Applying Terraform configuration..."
echo "This will recreate all deleted clusters"
echo ""

terraform apply -parallelism=128 -auto-approve 2>&1 | tee /tmp/terraform-apply-continued.log

echo ""
echo "✅ Fix process complete!"
