#!/usr/bin/env bash
set -euo pipefail

# Optional: set your subscription first if needed:
# az account set --subscription "$SUB"

REGIONS=(
  australiacentral
  australiaeast
  australiasoutheast
  austriaeast
  belgiumcentral
  brazilsouth
  canadacentral
  canadaeast
  centralindia
  chilecentral
  eastasia
  francecentral
  germanywestcentral
  indonesiacentral
  israelcentral
  italynorth
  japaneast
  japanwest
  koreacentral
  koreasouth
  malaysiawest
  mexicocentral
  newzealandnorth
  northeurope
  polandcentral
  qatarcentral
  southafricanorth
  southafricawest
  southeastasia
  southindia
  spaincentral
  switzerlandnorth
  switzerlandwest
  uaecentral
  uaenorth
  uksouth
  ukwest
  westeurope
  westindia
)

OUT_DIR=$(dirname "$0")/../reports
OUT_FILE="$OUT_DIR/dplsv6_usage.tsv"
mkdir -p "$OUT_DIR"
printf "Region\tName\tUsage\tLimit\n" > "$OUT_FILE"

for region in "${REGIONS[@]}"; do
  echo "Checking ${region}..." 1>&2
  # Use legacy VM usage API; filter any rows whose name contains Dpl and v6 (covers Dplsv6/Dpldsv6, etc.)
  # Output as TSV with columns: Name (localized), Usage, Limit
  az vm list-usage --location "${region}" \
    --output tsv \
    --query "[].{Name:name.localizedValue,Usage:currentValue,Limit:limit}" \
    | awk -v R="${region}" -F $'\t' 'tolower($1) ~ /dpl/ && tolower($1) ~ /v6/ { print R"\t"$1"\t"$2"\t"$3 }' \
    >> "${OUT_FILE}" || true
  # be gentle with API
  sleep 0.2
done

echo "Saved Dplsv6 usage report to: ${OUT_FILE}"
echo
echo "--- Dplsv6 usage per region (first 80 lines) ---"
column -t -s $'\t' "${OUT_FILE}" | sed -n '1,80p'


