#!/usr/bin/env bash
# Get vCPU quotas for all non-US commercial Azure regions

set -e

echo "=== Azure vCPU Quota Report - Non-US Commercial Regions ==="
echo ""
echo "Querying quotas for all non-US commercial regions..."
echo ""

# Output file
OUTPUT_FILE="docs/AZURE_VCPU_QUOTAS_ALL_REGIONS.md"

echo "# Azure vCPU Quota Report - All Non-US Commercial Regions" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "Generated: $(date -u +"%Y-%m-%d %H:%M:%S UTC")" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "## Summary" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "| Region | Display Name | Total vCPUs | Used | Available | Status |" >> "$OUTPUT_FILE"
echo "|--------|--------------|-------------|------|-----------|--------|" >> "$OUTPUT_FILE"

# Track totals
TOTAL_REGIONS=0
TOTAL_QUOTA=0
TOTAL_USED=0
TOTAL_AVAILABLE=0

# Define all non-US commercial regions
REGIONS=(
  "westeurope"
  "northeurope"
  "uksouth"
  "ukwest"
  "francecentral"
  "francesouth"
  "germanynorth"
  "germanywestcentral"
  "switzerlandnorth"
  "switzerlandwest"
  "italynorth"
  "norwayeast"
  "norwaywest"
  "polandcentral"
  "spaincentral"
  "swedencentral"
  "belgiumcentral"
  "australiaeast"
  "australiasoutheast"
  "eastasia"
  "southeastasia"
  "centralindia"
  "southindia"
  "westindia"
  "japaneast"
  "japanwest"
  "koreacentral"
  "koreasouth"
  "newzealandnorth"
  "malaysiawest"
  "indonesiacentral"
  "uaenorth"
  "uaecentral"
  "qatarcentral"
  "israelcentral"
  "southafricanorth"
  "southafricawest"
  "brazilsouth"
  "brazilsoutheast"
  "canadacentral"
  "canadaeast"
  "mexicocentral"
  "chilecentral"
)

# Query each region
for region in "${REGIONS[@]}"; do
  TOTAL_REGIONS=$((TOTAL_REGIONS + 1))
  
  echo -n "Querying $region... "
  
  # Get quota information - use JSON output for better parsing
  QUOTA_JSON=$(az vm list-usage --location "$region" --query "[?name.value=='cores']" -o json 2>/dev/null || echo "[]")
  
  if [ "$QUOTA_JSON" == "[]" ] || [ -z "$QUOTA_JSON" ]; then
    echo "❌ Not available or error"
    echo "| $region | N/A | N/A | N/A | N/A | ❌ Error/Not Available |" >> "$OUTPUT_FILE"
    continue
  fi
  
  # Parse JSON using jq if available, otherwise use awk
  if command -v jq &> /dev/null; then
    CURRENT=$(echo "$QUOTA_JSON" | jq -r '.[0].currentValue // 0')
    LIMIT=$(echo "$QUOTA_JSON" | jq -r '.[0].limit // 0')
  else
    # Fallback to awk parsing
    CURRENT=$(echo "$QUOTA_JSON" | grep -o '"currentValue":[0-9]*' | grep -o '[0-9]*' || echo "0")
    LIMIT=$(echo "$QUOTA_JSON" | grep -o '"limit":[0-9]*' | grep -o '[0-9]*' || echo "0")
  fi
  
  # Validate numbers
  if ! [[ "$CURRENT" =~ ^[0-9]+$ ]] || ! [[ "$LIMIT" =~ ^[0-9]+$ ]]; then
    echo "❌ Invalid data"
    echo "| $region | N/A | N/A | N/A | N/A | ❌ Invalid Data |" >> "$OUTPUT_FILE"
    continue
  fi
  
  AVAILABLE=$((LIMIT - CURRENT))
  
  # Calculate totals
  TOTAL_QUOTA=$((TOTAL_QUOTA + LIMIT))
  TOTAL_USED=$((TOTAL_USED + CURRENT))
  TOTAL_AVAILABLE=$((TOTAL_AVAILABLE + AVAILABLE))
  
  # Determine status
  if [ "$AVAILABLE" -ge 8 ]; then
    STATUS="✅ Sufficient (8+ available)"
  elif [ "$AVAILABLE" -ge 2 ]; then
    STATUS="⚠️ Limited (2-7 available)"
  else
    STATUS="❌ Insufficient (<2 available)"
  fi
  
  # Get region display name
  DISPLAY_NAME=$(az account list-locations --query "[?name=='$region'].displayName" -o tsv 2>/dev/null || echo "$region")
  
  echo "✅ $CURRENT/$LIMIT (Available: $AVAILABLE)"
  echo "| $region | $DISPLAY_NAME | $LIMIT | $CURRENT | $AVAILABLE | $STATUS |" >> "$OUTPUT_FILE"
done

echo "" >> "$OUTPUT_FILE"
echo "## Totals" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "- **Total Regions Queried**: $TOTAL_REGIONS" >> "$OUTPUT_FILE"
echo "- **Total vCPU Quota**: $TOTAL_QUOTA" >> "$OUTPUT_FILE"
echo "- **Total vCPUs Used**: $TOTAL_USED" >> "$OUTPUT_FILE"
echo "- **Total vCPUs Available**: $TOTAL_AVAILABLE" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "## Deployment Requirements" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "### Per-Region Requirements" >> "$OUTPUT_FILE"
echo "- **System Nodes**: 3 × Standard_D2s_v3 = 6 vCPUs" >> "$OUTPUT_FILE"
echo "- **Validator Nodes**: 1 × Standard_B2s = 2 vCPUs" >> "$OUTPUT_FILE"
echo "- **Total per Region**: 8 vCPUs" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "### Selected Regions for Deployment" >> "$OUTPUT_FILE"
echo "1. West Europe (westeurope)" >> "$OUTPUT_FILE"
echo "2. North Europe (northeurope)" >> "$OUTPUT_FILE"
echo "3. UK South (uksouth)" >> "$OUTPUT_FILE"
echo "4. France Central (francecentral)" >> "$OUTPUT_FILE"
echo "5. Germany West Central (germanywestcentral)" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "## Notes" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "- Quota is per-region and per-subscription" >> "$OUTPUT_FILE"
echo "- Some regions may not be available in all subscriptions" >> "$OUTPUT_FILE"
echo "- Quota can be increased via Azure Portal or support request" >> "$OUTPUT_FILE"
echo "- Standard_B2s requires 2 vCPUs per validator node" >> "$OUTPUT_FILE"
echo "- Standard_D2s_v3 requires 2 vCPUs per system node" >> "$OUTPUT_FILE"

echo ""
echo "✅ Quota report generated: $OUTPUT_FILE"
