# Multi-Region Deployment Configuration (LEGACY)
#
# IMPORTANT:
# - This file captures earlier experiments with Dv2/Dsv6/DSv3 families and a
#   small set of regions.
# - The **active, quota-safe 37-region Dplsv6 design** now lives in
#   `multi-region-global.tf` and is controlled via `enable_multi_region = true`
#   in `terraform.tfvars`.
# - This legacy path is gated by `enable_multi_region_legacy` and should
#   normally remain **false**. Treat it as historical reference only; do not
#   enable it in new deployments.


locals {
  # Multi-region configuration with VM family selection per region
  # References: local.cloud_provider and local.env_code from locals.tf
  multi_region_config = {
    westeurope = {
      location      = "westeurope"
      region_code   = "we"
      # Available quotas: DSv3 (4), D Family (10), Dv2 (10), Dsv6 (10), Av2 (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family (10 available)
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family (10 available)
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family (10 available)
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family (10 available)
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs (Dv2)
        validators = 1  # 1 × 4 = 4 vCPUs (Dv2)
        sentries   = 1  # 1 × 4 = 4 vCPUs (Dv2)
        rpc        = 1  # 1 × 8 = 8 vCPUs (Dsv6)
      }
      # Total: 18 vCPUs (Dv2: 10, Dsv6: 8 - within quotas)
    }
    northeurope = {
      location      = "northeurope"
      region_code   = "ne"
      # Available quotas: Dv2 (10), Dsv6 (10), Av2 (10), D Family (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs
        validators = 1  # 1 × 4 = 4 vCPUs
        sentries   = 1  # 1 × 4 = 4 vCPUs
        rpc        = 1  # 1 × 8 = 8 vCPUs
      }
      # Total: 18 vCPUs
    }
    francecentral = {
      location      = "francecentral"
      region_code   = "fc"
      # Available quotas: Dv2 (10), Dsv6 (10), Av2 (10), D Family (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs
        validators = 1  # 1 × 4 = 4 vCPUs
        sentries   = 1  # 1 × 4 = 4 vCPUs
        rpc        = 1  # 1 × 8 = 8 vCPUs
      }
      # Total: 18 vCPUs
    }
    uksouth = {
      location      = "uksouth"
      region_code   = "uk"
      # Available quotas: Dv2 (10), Dsv6 (10), Av2 (10), D Family (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs (Dv2)
        validators = 1  # 1 × 4 = 4 vCPUs (Dv2)
        sentries   = 1  # 1 × 4 = 4 vCPUs (Dv2)
        rpc        = 1  # 1 × 8 = 8 vCPUs (Dsv6)
      }
      # Total: 18 vCPUs (Dv2: 10, Dsv6: 8 - within quotas)
    }
    swedencentral = {
      location      = "swedencentral"
      region_code   = "sc"
      # Available quotas: Dv2 (10), Dsv6 (10), Av2 (10), D Family (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs (Dv2)
        validators = 1  # 1 × 4 = 4 vCPUs (Dv2)
        sentries   = 1  # 1 × 4 = 4 vCPUs (Dv2)
        rpc        = 1  # 1 × 8 = 8 vCPUs (Dsv6)
      }
      # Total: 18 vCPUs (Dv2: 10, Dsv6: 8 - within quotas)
    }
    norwayeast = {
      location      = "norwayeast"
      region_code   = "no"  # Using "no" to avoid conflict with northeurope "ne"
      # Available quotas: Dv2 (10), Dsv6 (10), Av2 (10), D Family (10)
      vm_families = {
        system     = "Standard_D2_v2"       # 2 vCPUs - Use Dv2 Family
        validators = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        sentries   = "Standard_D4_v2"       # 4 vCPUs - Use Dv2 Family
        rpc        = "Standard_D8s_v6"      # 8 vCPUs - Use Dsv6 Family
      }
      node_count = {
        system     = 1  # 1 × 2 = 2 vCPUs (Dv2)
        validators = 1  # 1 × 4 = 4 vCPUs (Dv2)
        sentries   = 1  # 1 × 4 = 4 vCPUs (Dv2)
        rpc        = 1  # 1 × 8 = 8 vCPUs (Dsv6)
      }
      # Total: 18 vCPUs (Dv2: 10, Dsv6: 8 - within quotas)
    }
  }
}

# Multi-region resource groups (LEGACY)
resource "azurerm_resource_group" "multi_region" {
  for_each = var.enable_multi_region_legacy ? local.multi_region_config : {}
  
  name     = "${local.cloud_provider}-${local.env_code}-${each.value.region_code}-rg-comp-001"
  location = each.value.location
  
  tags = merge(local.common_tags, {
    Region = each.value.location
    Deployment = "multi-region"
  })
}

# Multi-region AKS clusters (LEGACY)
module "aks_multi_region" {
  for_each = var.enable_multi_region_legacy ? local.multi_region_config : {}
  source   = "./modules/kubernetes"
  
  resource_group_name = azurerm_resource_group.multi_region[each.key].name
  location            = each.value.location
  cluster_name        = "${local.cloud_provider}-${local.env_code}-${each.value.region_code}-aks-main"
  kubernetes_version  = var.kubernetes_version
  node_count          = each.value.node_count
  vm_size             = each.value.vm_families
  environment         = var.environment
  tags                = merge(local.common_tags, {
    Region = each.value.location
    Deployment = "multi-region"
  })
  
  # Use existing networking or create per-region
  vnet_subnet_id = module.networking_multi_region[each.key].aks_subnet_id
  node_subnet_id = module.networking_multi_region[each.key].node_subnet_id
  
  # Shared Key Vault (or per-region)
  key_vault_id = module.keyvault.key_vault_id
  
  depends_on = [
    azurerm_resource_group.multi_region,
    module.networking_multi_region
  ]
}

# Multi-region networking (LEGACY)
module "networking_multi_region" {
  for_each = var.enable_multi_region_legacy ? local.multi_region_config : {}
  source   = "./modules/networking"
  
  resource_group_name = azurerm_resource_group.multi_region[each.key].name
  location            = each.value.location
  cluster_name        = "${local.cloud_provider}-${local.env_code}-${each.value.region_code}-aks-main"
  environment         = var.environment
  tags                = merge(local.common_tags, {
    Region = each.value.location
    Deployment = "multi-region"
  })
}

# Multi-region storage (LEGACY)
module "storage_multi_region" {
  for_each = var.enable_multi_region_legacy ? local.multi_region_config : {}
  source   = "./modules/storage"
  
  resource_group_name = azurerm_resource_group.multi_region[each.key].name
  location            = each.value.location
  cluster_name        = "${local.cloud_provider}-${local.env_code}-${each.value.region_code}-aks-main"
  environment         = var.environment
  tags                = merge(local.common_tags, {
    Region = each.value.location
    Deployment = "multi-region"
  })
}

# Outputs for multi-region deployment (LEGACY)
output "multi_region_clusters" {
  value = var.enable_multi_region_legacy ? {
    for k, v in module.aks_multi_region : k => {
      cluster_name = v.cluster_name
      cluster_fqdn = v.cluster_fqdn
      location     = local.multi_region_config[k].location
    }
  } : {}
  description = "Multi-region AKS cluster information"
}

