# AKS Cluster Deployment (Phase 2)
# Only deploy if deploy_aks_clusters is true

# Map regions to VM sizes based on availability
# Some regions don't support Arm-based VM sizes (D2plsv6/D2psv6)
# Use x86-based alternatives (D4s_v3/D2s_v3) as fallback
locals {
  # Regions that don't support Standard_D2plsv6
  regions_without_arm_system = [
    "eastasia",
    "qatarcentral",
    "australiasoutheast",
    "southindia"
  ]
  
  # Regions that don't support Standard_D2psv6
  regions_without_arm_validator = [
    "eastasia",
    "qatarcentral",
    "australiasoutheast",
    "southindia"
  ]
  
  # System node VM size: Use Arm-based if available, fallback to x86
  # Use Standard_D2plsv6 (2 vCPUs) for system pools where supported to stay within 10 vCPUs/region.
  # Fallback to D4s_v3 in regions without Arm support.
  system_vm_size = contains(local.regions_without_arm_system, var.region_name) ? "Standard_D4s_v3" : "Standard_D2plsv6"
  
  # Validator node VM size: Use Arm-based if available, fallback to x86
  validator_vm_size_override = contains(local.regions_without_arm_validator, var.region_name) ? "Standard_D2s_v3" : null
}

resource "azurerm_kubernetes_cluster" "main" {
  count = var.deploy_aks_clusters ? 1 : 0

  name                = "${var.cloud_provider}-${var.env_code}-${var.region_code}-aks-main"
  location            = var.region_name
  resource_group_name = azurerm_resource_group.compute.name
  dns_prefix          = "${var.cloud_provider}-${var.env_code}-${var.region_code}-aks"
  # Use latest stable version (will be set to default available version per region)
  # For better compatibility, using 1.32 which is widely supported
  kubernetes_version = "1.32"

  # Network configuration
  network_profile {
    network_plugin    = "azure"
    network_policy    = "azure"
    load_balancer_sku = "standard"
    service_cidr      = "10.1.0.0/16"
    dns_service_ip    = "10.1.0.10"
  }

  # Default node pool (system)
  # For 36-region blueprint: 2 system nodes per region
  default_node_pool {
    name                = "system"
    node_count          = 2 # Updated for 36-region blueprint (2 per region)
    vm_size             = local.system_vm_size  # Region-specific VM size (4 vCPUs, 8-16 GB RAM)
    vnet_subnet_id      = azurerm_subnet.aks.id
    enable_auto_scaling = false
    os_disk_size_gb     = 128
    type                = "VirtualMachineScaleSets"

    node_labels = {
      pool = "system"
    }

    tags = merge(var.common_tags, {
      Pool = "system"
    })
  }

  # Identity
  identity {
    type = "SystemAssigned"
  }

  # Azure Monitor
  oms_agent {
    log_analytics_workspace_id = azurerm_log_analytics_workspace.main.id
  }

  tags = merge(var.common_tags, {
    Purpose = "Kubernetes-Cluster"
    Region  = var.region_display
  })
}

# Outputs
output "aks_cluster" {
  value = var.deploy_aks_clusters ? {
    cluster_name = azurerm_kubernetes_cluster.main[0].name
    fqdn         = azurerm_kubernetes_cluster.main[0].fqdn
    kube_config  = azurerm_kubernetes_cluster.main[0].kube_config_raw
  } : null
}

# Validator node pool is defined in validators.tf
# Output is defined in validators.tf
