# Local values for naming convention
# Implements: {cloud}-{env}-{region}-{resource}-{instance}

locals {
  # Cloud provider
  cloud_provider = "az"
  
  # Environment code mapping
  env_codes = {
    prod    = "p"
    dev     = "d"
    test    = "t"
    staging = "s"
  }
  
  # Region code mapping for all non-US Commercial Azure regions
  region_codes = {
    belgiumcentral      = "bel"
    brazilsouth         = "bra"
    brazilsoutheast     = "bse"
    canadacentral       = "can"
    canadaeast          = "cae"
    centralindia        = "cin"
    chilecentral        = "chc"
    eastasia            = "eas"
    francecentral       = "frc"
    francesouth         = "frs"
    germanynorth        = "gno"
    germanywestcentral  = "gwc"
    indonesiacentral    = "idn"
    israelcentral       = "ilc"
    italynorth          = "ita"
    japaneast           = "jpe"
    japanwest           = "jpw"
    jioindiacentral     = "jic"
    jioindiawest        = "jiw"
    koreacentral        = "kor"
    koreasouth          = "kos"
    malaysiawest        = "mys"
    mexicocentral       = "mex"
    newzealandnorth     = "nzl"
    northeurope         = "nor"
    norwayeast          = "noe"
    norwaywest          = "now"
    polandcentral       = "pol"
    qatarcentral        = "qat"
    southafricanorth    = "san"
    southafricawest     = "saw"
    southeastasia       = "sea"
    southindia          = "sin"
    spaincentral        = "spa"
    swedencentral       = "swc"
    switzerlandnorth    = "swn"
    switzerlandwest     = "swt"
    uaecentral          = "uac"
    uaenorth            = "uan"
    uksouth             = "uks"
    ukwest              = "ukw"
    westeurope          = "wst"
    westindia           = "win"
  }
  
  # Current codes
  env_code    = local.env_codes[var.environment]
  region_code = lookup(local.region_codes, var.location, "we")
  
  # Naming prefix: {cloud}-{env}-{region}
  name_prefix = "${local.cloud_provider}-${local.env_code}-${local.region_code}"
  
  # Resource Group Names
  rg_network  = "${local.name_prefix}-rg-net-001"
  rg_compute  = "${local.name_prefix}-rg-comp-001"
  rg_storage  = "${local.name_prefix}-rg-stor-001"
  rg_security = "${local.name_prefix}-rg-sec-001"
  rg_monitor  = "${local.name_prefix}-rg-mon-001"
  
  # Compute Resources
  aks_cluster = "${local.name_prefix}-aks-main"
  aks_node_sys   = "${local.name_prefix}-aks-node-sys"
  aks_node_valid = "${local.name_prefix}-aks-node-valid"
  aks_node_sent  = "${local.name_prefix}-aks-node-sent"
  aks_node_rpc   = "${local.name_prefix}-aks-node-rpc"
  
  # Networking Resources
  vnet_main     = "${local.name_prefix}-vnet-main"
  snet_aks      = "${local.name_prefix}-snet-aks"
  snet_valid    = "${local.name_prefix}-snet-valid"
  snet_sent     = "${local.name_prefix}-snet-sent"
  snet_rpc      = "${local.name_prefix}-snet-rpc"
  snet_agw      = "${local.name_prefix}-snet-agw"
  nsg_valid     = "${local.name_prefix}-nsg-valid"
  nsg_sent      = "${local.name_prefix}-nsg-sent"
  nsg_rpc       = "${local.name_prefix}-nsg-rpc"
  pip_agw       = "${local.name_prefix}-pip-agw"
  agw_main      = "${local.name_prefix}-agw-main"
  
  # Storage Resources
  st_backup     = "${local.name_prefix}-st-backup-001"
  st_shared     = "${local.name_prefix}-st-shared-001"
  st_tfstate    = "${local.name_prefix}-st-tfstate-001"
  cnt_chaindata = "${local.name_prefix}-cnt-chaindata"
  cnt_config    = "${local.name_prefix}-cnt-config"
  
  # Security Resources
  kv_secrets    = "${local.name_prefix}-kv-secrets-001"
  id_aks        = "${local.name_prefix}-id-aks"
  id_oracle     = "${local.name_prefix}-id-oracle"
  
  # Monitoring Resources
  law_main      = "${local.name_prefix}-law-main"
  appi_main     = "${local.name_prefix}-appi-main"
  ag_alerts     = "${local.name_prefix}-ag-alerts"
  
  # Use Well-Architected naming if enabled, otherwise use legacy naming
  resource_group_name = var.use_well_architected ? "" : (
    var.resource_group_name != "" ? var.resource_group_name : local.rg_compute
  )
  
  # Comprehensive tags
  common_tags = merge(
    var.tags,
    {
      Environment = var.environment
      CostCenter  = "Blockchain"
      Owner       = "DevOps Team"
      NamingConvention = "az-env-region-resource-instance"
    }
  )
}

