#!/usr/bin/env bash
# Check Azure Quotas for specific region
# Usage: ./scripts/azure/check-quotas.sh [region]

set -euo pipefail

# Colors

REGION="${1:-westeurope}"

# VM Sizes and required counts
declare -A VM_REQUIREMENTS=(
    ["StandardD2sv3Family"]=3
    ["StandardD4sv3Family"]=7
    ["StandardD8sv3Family"]=3
)

log() {
    log_success "[✓] $1"
}

error() {
    log_error "[✗] $1"
}

warn() {
    log_warn "[!] $1"
}

info() {
    log_info "[i] $1"
}

section() {
    echo
    log_info "=== $1 ==="
}

section "Quota Check for Region: $REGION"

# Check VM Family Quotas
info "Checking VM Family Quotas..."

for family in "${!VM_REQUIREMENTS[@]}"; do
    needed=${VM_REQUIREMENTS[$family]}
    
    # Get quota
    quota_info=$(az vm list-usage \
        --location "$REGION" \
        --query "[?name.value=='$family'].{Name:name.value, Current:currentValue, Limit:limit}" \
        -o json 2>/dev/null || echo "[]")
    
    if [ "$quota_info" != "[]" ] && [ -n "$quota_info" ]; then
        current=$(echo "$quota_info" | jq -r '.[0].Current // 0')
        limit=$(echo "$quota_info" | jq -r '.[0].Limit // 0')
        available=$((limit - current))
        
        if [ $available -ge $needed ]; then
            log "$family: $current/$limit used, $available available (need $needed) ✓"
        else
            error "$family: $current/$limit used, only $available available (need $needed) ✗"
            warn "  Request quota increase: az vm list-usage --location $REGION"
        fi
    else
        warn "$family: Quota information not available"
    fi
done

# Check AKS Quota
section "AKS Cluster Quota"
aks_count=$(az aks list --query "length(@)" -o tsv 2>/dev/null || echo "0")
log "Current AKS clusters in subscription: $aks_count"
info "Typical limit: 50 clusters per subscription"

# Check Network Quotas
section "Network Resource Quotas"

# Public IPs
pip_info=$(az network list-usages \
    --location "$REGION" \
    --query "[?name.value=='PublicIPAddresses'].{Name:name.value, Current:currentValue, Limit:limit}" \
    -o json 2>/dev/null || echo "[]")

if [ "$pip_info" != "[]" ] && [ -n "$pip_info" ]; then
    pip_current=$(echo "$pip_info" | jq -r '.[0].Current // 0')
    pip_limit=$(echo "$pip_info" | jq -r '.[0].Limit // 0')
    pip_available=$((pip_limit - pip_current))
    needed_pip=5  # Approximate need
    
    if [ $pip_available -ge $needed_pip ]; then
        log "Public IPs: $pip_current/$pip_limit used, $pip_available available (need ~$needed_pip) ✓"
    else
        error "Public IPs: $pip_current/$pip_limit used, only $pip_available available (need ~$needed_pip) ✗"
    fi
else
    warn "Public IP quota information not available"
fi

# Load Balancers
lb_info=$(az network list-usages \
    --location "$REGION" \
    --query "[?name.value=='LoadBalancers'].{Name:name.value, Current:currentValue, Limit:limit}" \
    -o json 2>/dev/null || echo "[]")

if [ "$lb_info" != "[]" ] && [ -n "$lb_info" ]; then
    lb_current=$(echo "$lb_info" | jq -r '.[0].Current // 0')
    lb_limit=$(echo "$lb_info" | jq -r '.[0].Limit // 0')
    lb_available=$((lb_limit - lb_current))
    needed_lb=2  # Approximate need
    
    if [ $lb_available -ge $needed_lb ]; then
        log "Load Balancers: $lb_current/$lb_limit used, $lb_available available (need ~$needed_lb) ✓"
    else
        error "Load Balancers: $lb_current/$lb_limit used, only $lb_available available (need ~$needed_lb) ✗"
    fi
else
    warn "Load Balancer quota information not available"
fi

# Network Interfaces
nic_info=$(az network list-usages \
    --location "$REGION" \
    --query "[?name.value=='NetworkInterfaces'].{Name:name.value, Current:currentValue, Limit:limit}" \
    -o json 2>/dev/null || echo "[]")

if [ "$nic_info" != "[]" ] && [ -n "$nic_info" ]; then
    nic_current=$(echo "$nic_info" | jq -r '.[0].Current // 0')
    nic_limit=$(echo "$nic_info" | jq -r '.[0].Limit // 0')
    nic_available=$((nic_limit - nic_current))
    needed_nic=20  # Approximate need
    
    if [ $nic_available -ge $needed_nic ]; then
        log "Network Interfaces: $nic_current/$nic_limit used, $nic_available available (need ~$needed_nic) ✓"
    else
        error "Network Interfaces: $nic_current/$nic_limit used, only $nic_available available (need ~$needed_nic) ✗"
    fi
else
    warn "Network Interface quota information not available"
fi

# Storage Accounts
section "Storage Account Quota"
storage_info=$(az storage account list --query "length(@)" -o tsv 2>/dev/null || echo "0")
log "Current storage accounts in subscription: $storage_info"
info "Typical limit: 250 storage accounts per subscription"

echo
section "Summary"
info "Region: $REGION"
info "Review quotas above and request increases if needed"
info "To request quota increase, visit: https://portal.azure.com/#blade/Microsoft_Azure_Support/HelpAndSupportBlade/managesupportrequest"

