#!/bin/bash
# Generated by: Claude AI via Cursor
set -x

# This script orchestrates the deletion of both the hosted cluster and AKS management cluster.
# It follows the same pattern as setup_all.sh but for deletion operations.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Load variables - check if vars.sh is available
if [[ -f "${SCRIPT_DIR}/vars.sh" ]]; then
    source "${SCRIPT_DIR}/vars.sh"
else
    echo "Error: vars.sh not found. Please ensure you're running this from the managed-azure directory."
    exit 1
fi

# Check if user is logged into Azure, and log in if not
if ! az account show >/dev/null 2>&1; then
  echo "Not logged into Azure. Logging in now..."
  az login --scope https://management.core.windows.net//.default
  if [ $? -ne 0 ]; then
    echo "Error: Azure login failed. Please check your credentials."
    exit 1
  fi
  echo "Successfully logged into Azure!"
fi

echo "Starting deletion process..."
echo "This will delete:"
echo "  1. Hosted cluster: ${PREFIX}-hc"
echo "  2. AKS management cluster: ${AKS_CLUSTER_NAME}"
echo "  3. Cluster-specific Azure resource groups"
echo ""
echo "This will PRESERVE for reuse:"
echo "  • Managed identities in ${PERSISTENT_RG_NAME}"
echo "  • Service principals and Key Vault"
echo "  • OIDC provider resources"
echo ""

# Add a confirmation prompt for safety
read -p "Are you sure you want to proceed with deletion? This action cannot be undone. (yes/no): " -r
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
    echo "Deletion cancelled by user."
    exit 0
fi

# Step 1: Delete the hosted cluster first
echo "Step 1: Deleting hosted cluster..."
"${SCRIPT_DIR}/delete_hosted_cluster.sh"
if [ $? -ne 0 ]; then
    echo "Error: Failed to delete hosted cluster. Stopping deletion process."
    echo "You may need to manually clean up resources."
    exit 1
fi

# Wait a moment before proceeding to AKS deletion
echo "Waiting 30 seconds before proceeding to AKS cluster deletion..."
sleep 30

# Step 2: Delete the AKS management cluster
echo "Step 2: Deleting AKS management cluster..."
"${SCRIPT_DIR}/delete_aks_cluster.sh"
if [ $? -ne 0 ]; then
    echo "Error: Failed to delete AKS cluster. Some manual cleanup may be required."
    echo "Please check the Azure portal for any remaining resources."
    exit 1
fi

echo ""
echo "================================================================"
echo "Deletion process completed successfully!"
echo ""
echo "The following resources have been deleted or are being deleted:"
echo "  ✓ Hosted cluster: ${PREFIX}-hc"
echo "  ✓ AKS management cluster: ${AKS_CLUSTER_NAME}"
echo "  ✓ Managed resource group: ${PREFIX}-managed-rg"
echo "  ✓ Customer VNET resource group: ${PREFIX}-customer-vnet-rg"
echo "  ✓ Customer NSG resource group: ${PREFIX}-customer-nsg-rg"
echo "  ✓ AKS resource group: ${AKS_RG}"
echo ""
echo "The following resources are preserved for reuse:"
echo "  ↻ Managed identities in ${PERSISTENT_RG_NAME}"
echo "  ↻ Service principals and Key Vault"
echo "  ↻ OIDC provider resources"
echo ""
echo "Note: AKS-specific Key Vault role assignments are removed"
echo ""
echo "Note: Some resource deletions may still be in progress."
echo "You can monitor the status in the Azure portal or with:"
echo "  az group list --query \"[?starts_with(name, '$PREFIX')]\""
echo "================================================================"

set +x