#!/usr/bin/env bash
# Region code mapping - Single source of truth
# All region codes are standardized to exactly 3 characters
# Usage: source "$SCRIPT_DIR/lib/config/regions.sh"

# Region codes mapping (standardized to exactly 3 characters)
declare -A REGION_CODES=(
    ["northeurope"]="nor"
    ["uksouth"]="uks"
    ["ukwest"]="ukw"
    ["westeurope"]="wst"
    ["francecentral"]="frc"
    ["germanywestcentral"]="gwc"
    ["switzerlandnorth"]="swn"
    ["switzerlandwest"]="swt"
    ["swedencentral"]="swc"
    ["norwayeast"]="noe"
    ["polandcentral"]="pol"
    ["spaincentral"]="spa"
    ["italynorth"]="ita"
    ["southindia"]="sin"
    ["centralindia"]="cin"
    ["westindia"]="win"
    ["belgiumcentral"]="bel"
    ["eastasia"]="eas"
    ["southeastasia"]="sea"
    ["japaneast"]="jpe"
    ["japanwest"]="jpw"
    ["koreacentral"]="kor"
    ["koreasouth"]="kos"
    ["australiaeast"]="aus"
    ["australiasoutheast"]="ase"
    ["newzealandnorth"]="nzl"
    ["indonesiacentral"]="idn"
    ["malaysiawest"]="mys"
    ["uaenorth"]="uae"
    ["qatarcentral"]="qat"
    ["israelcentral"]="ilc"
    ["canadacentral"]="can"
    ["canadaeast"]="cae"
    ["brazilsouth"]="bra"
    ["chilecentral"]="chl"
    ["mexicocentral"]="mex"
    ["southafricanorth"]="zaf"
    ["austriaeast"]="aut"
)

# Reverse mapping (code -> region name)
declare -A REGION_CODE_TO_NAME=()
for region_name in "${!REGION_CODES[@]}"; do
    REGION_CODE_TO_NAME["${REGION_CODES[$region_name]}"]="$region_name"
done

# Old code mappings (for backward compatibility with existing resources)
declare -A OLD_CODE_TO_REGION=(
    ["ne"]="northeurope"
    ["we"]="westeurope"
    ["fc"]="francecentral"
    ["sn"]="switzerlandnorth"
    ["sw"]="switzerlandwest"
    ["in"]="italynorth"
    ["pc"]="polandcentral"
    ["sc"]="spaincentral"
    ["bc"]="belgiumcentral"
    ["ae"]="australiaeast"  # Note: conflicts with austriaeast (old), prefer australiaeast
    ["ea"]="eastasia"
    ["ci"]="centralindia"
    ["si"]="southindia"
    ["wi"]="westindia"
    ["je"]="japaneast"
    ["jw"]="japanwest"
    ["kc"]="koreacentral"
    ["ks"]="koreasouth"
    ["cc"]="canadacentral"
    ["ce"]="canadaeast"
    ["bs"]="brazilsouth"
    ["mc"]="mexicocentral"
    ["qc"]="qatarcentral"
    ["ic"]="indonesiacentral"
    ["mw"]="malaysiawest"
    ["nzn"]="newzealandnorth"
    ["san"]="southafricanorth"
    ["uan"]="uaenorth"
    ["chc"]="chilecentral"
)

# Get all regions as array (region_name:code format)
get_all_regions() {
    for region_name in "${!REGION_CODES[@]}"; do
        echo "${region_name}:${REGION_CODES[$region_name]}"
    done | sort
}

# Get region code by region name
get_region_code() {
    local region_name="$1"
    echo "${REGION_CODES[$region_name]:-}"
}

# Get region name by code (tries new codes first, then old codes)
get_region_name() {
    local code="$1"
    # Try new codes first
    if [ -n "${REGION_CODE_TO_NAME[$code]:-}" ]; then
        echo "${REGION_CODE_TO_NAME[$code]}"
        return 0
    fi
    # Try old codes for backward compatibility
    if [ -n "${OLD_CODE_TO_REGION[$code]:-}" ]; then
        echo "${OLD_CODE_TO_REGION[$code]}"
        return 0
    fi
    return 1
}

# Validate region code (must be 3 characters)
is_valid_region_code() {
    local code="$1"
    [[ "$code" =~ ^[a-z]{3}$ ]] && [ -n "${REGION_CODE_TO_NAME[$code]:-}" ]
}

