# Local values for naming conventions and common configurations
# Follows standard naming pattern: {provider}-{region}-{resource}-{env}-{purpose}

locals {
  # Provider identifier
  provider = "az"

  # Region abbreviation mapping
  region_abbrev = {
    westeurope         = "we"
    northeurope        = "ne"
    uksouth            = "uk"
    switzerlandnorth    = "ch"
    norwayeast         = "no"
    francecentral      = "fr"
    germanywestcentral = "de"
  }

  # Current region abbreviation
  region_short = lookup(local.region_abbrev, var.azure_region, "we")

  # Environment abbreviations
  env_abbrev = {
    dev  = "dev"
    stage = "stg"
    prod = "prd"
    mgmt = "mgmt"
  }

  # Current environment abbreviation
  env_short = lookup(local.env_abbrev, var.environment, "dev")

  # Project name (shortened for resource names)
  project_short = "ord"

  # Naming functions
  # Format: {provider}-{region}-{resource}-{env}-{purpose}
  name_prefix = "${local.provider}-${local.region_short}"

  # Resource Group naming
  # Pattern: az-we-rg-dev-main
  rg_name = "${local.name_prefix}-rg-${local.env_short}-main"
  rg_state_name = "${local.name_prefix}-rg-${local.env_short}-state"

  # Storage Account naming (alphanumeric only, max 24 chars)
  # Pattern: azwesadevdata (az + we + sa + dev + data)
  sa_data_name = "${local.provider}${local.region_short}sa${local.env_short}data"
  sa_state_name = "${local.provider}${local.region_short}sa${local.env_short}state"
  sa_cdn_name   = "${local.provider}${local.region_short}sa${local.env_short}cdn"

  # Key Vault naming (alphanumeric and hyphens, max 24 chars)
  # Pattern: az-we-kv-dev-main
  kv_name = "${local.name_prefix}-kv-${local.env_short}-main"

  # AKS Cluster naming (max 63 chars)
  # Pattern: az-we-aks-dev-main
  aks_name = "${local.name_prefix}-aks-${local.env_short}-main"

  # Container Registry naming (alphanumeric only, max 50 chars)
  # Pattern: azweacrdev (az + we + acr + dev)
  acr_name = "${local.provider}${local.region_short}acr${local.env_short}"

  # PostgreSQL Server naming (max 63 chars)
  # Pattern: az-we-psql-dev-main
  psql_name = "${local.name_prefix}-psql-${local.env_short}-main"

  # Database naming (max 63 chars)
  # Pattern: az-we-db-dev-main
  db_name = "${local.name_prefix}-db-${local.env_short}-main"

  # Virtual Network naming (max 64 chars)
  # Pattern: az-we-vnet-dev-main
  vnet_name = "${local.name_prefix}-vnet-${local.env_short}-main"

  # Application Insights naming (max 255 chars)
  # Pattern: az-we-appi-dev-main
  appi_name = "${local.name_prefix}-appi-${local.env_short}-main"

  # Log Analytics Workspace naming (max 63 chars)
  # Pattern: az-we-law-dev-main
  law_name = "${local.name_prefix}-law-${local.env_short}-main"

  # Common tags
  common_tags = {
    Environment = var.environment
    Project     = var.project_name
    Region      = var.azure_region
    ManagedBy   = "Terraform"
    CostCenter  = "engineering"
    Owner       = "platform-team"
  }
}

