# Azure Infrastructure Module
# Adapts existing Azure modules for multi-cloud architecture

locals {
  env = var.environment_config
  
  # Extract Azure-specific config
  azure_config = try(local.env.azure, {})
  
  # Extract infrastructure config
  infra = try(local.env.infrastructure, {})
  k8s_config = try(local.infra.kubernetes, {})
  net_config = try(local.infra.networking, {})
  
  # Naming
  name_prefix = "${local.env.name}-${var.environment}"
  
  # Node pools
  node_pools = try(local.k8s_config.node_pools, {})
  
  # Region
  location = try(local.env.region, "westeurope")
}

# Resource Group
resource "azurerm_resource_group" "main" {
  name     = try(local.azure_config.resource_group_name, "${local.name_prefix}-rg")
  location = local.location
  
  tags = var.tags
}

# Network Module (reuse existing)
module "networking" {
  source = "../../modules/networking"
  
  resource_group_name = azurerm_resource_group.main.name
  location            = local.location
  cluster_name        = "${local.name_prefix}-aks"
  environment         = var.environment
  tags                = var.tags
}

# Key Vault Module (reuse existing)
module "keyvault" {
  source = "../../modules/secrets"
  
  resource_group_name = azurerm_resource_group.main.name
  location            = local.location
  key_vault_name      = try(local.env.secrets.key_vault_name, "${local.name_prefix}-kv")
  environment         = var.environment
  tags                = var.tags
}

# AKS Module (reuse existing, with modifications)
module "aks" {
  source = "../../modules/kubernetes"
  
  resource_group_name = azurerm_resource_group.main.name
  location            = local.location
  cluster_name        = "${local.name_prefix}-aks"
  kubernetes_version  = try(local.k8s_config.version, "1.28")
  
  # Convert node_pools config to node_count and vm_size format
  node_count = {
    system     = try(local.node_pools.system.count, 1)
    validators = try(local.node_pools.validators.count, 0)
    sentries   = try(local.node_pools.sentries.count, 0)
    rpc        = try(local.node_pools.rpc.count, 0)
  }
  
  vm_size = {
    system     = try(local.node_pools.system.vm_size, "Standard_D2s_v3")
    validators = try(local.node_pools.validators.vm_size, "Standard_D4s_v3")
    sentries   = try(local.node_pools.sentries.vm_size, "Standard_D4s_v3")
    rpc        = try(local.node_pools.rpc.vm_size, "Standard_D8s_v3")
  }
  
  environment = var.environment
  tags        = var.tags
  
  vnet_subnet_id = module.networking.aks_subnet_id
  node_subnet_id = module.networking.node_subnet_id
  key_vault_id   = module.keyvault.key_vault_id
  
  depends_on = [
    module.networking,
    module.keyvault
  ]
}

# Storage Module (reuse existing)
module "storage" {
  source = "../../modules/storage"
  
  resource_group_name = azurerm_resource_group.main.name
  location            = local.location
  cluster_name        = "${local.name_prefix}-aks"
  environment         = var.environment
  tags                = var.tags
}

# Azure Arc onboarding (if enabled)
resource "azapi_resource" "arc_cluster" {
  count = try(local.env.azure.arc_enabled, false) ? 1 : 0
  
  type      = "Microsoft.Kubernetes/connectedClusters@2023-11-01-preview"
  name      = "${local.name_prefix}-arc"
  location  = local.location
  
  parent_id = azurerm_resource_group.main.id
  
  body = jsonencode({
    properties = {
      agentPublicKeyCertificate = ""  # Will be populated by Arc agent
      distribution              = "AKS"
      infrastructure            = "azure"
    }
  })
  
  tags = var.tags
}

