# Nomad 多数据中心集群输出

# 集群概览
output "cluster_overview" {
  description = "Nomad 多数据中心集群概览"
  value = {
    datacenters = {
      dc1 = {
        name     = "dc1"
        location = "China (CN)"
        provider = "existing"
        node     = "semaphore"
        ip       = "100.116.158.95"
        status   = "existing"
      }
      dc2 = var.deploy_korea_node ? {
        name     = "dc2"
        location = "Korea (KR)"
        provider = "oracle"
        node     = "ch4"
        ip       = try(oci_core_instance.nomad_kr_node[0].public_ip, "pending")
        status   = "deployed"
      } : null
      dc3 = var.deploy_us_node ? {
        name     = "dc3"
        location = "US"
        provider = "aws" # 暂时使用AWS替代华为云
        node     = "ash3c"
        ip       = "pending" # 暂时禁用
        status   = "disabled"
      } : null
    }
    total_nodes = 1 + (var.deploy_korea_node ? 1 : 0) + (var.deploy_us_node ? 1 : 0)
  }
}

# Oracle Cloud 韩国节点输出
output "oracle_korea_node" {
  description = "Oracle Cloud 韩国节点信息"
  value = var.deploy_korea_node ? {
    instance_id = try(oci_core_instance.nomad_kr_node[0].id, null)
    public_ip   = try(oci_core_instance.nomad_kr_node[0].public_ip, null)
    private_ip  = try(oci_core_instance.nomad_kr_node[0].private_ip, null)
    datacenter  = "dc2"
    provider    = "oracle"
    region      = var.oracle_config.region
    
    # 连接信息
    ssh_command = try("ssh ubuntu@${oci_core_instance.nomad_kr_node[0].public_ip}", null)
    nomad_ui    = try("http://${oci_core_instance.nomad_kr_node[0].public_ip}:4646", null)
  } : null
}

# 华为云美国节点输出 - 暂时禁用
# output "huawei_us_node" {
#   description = "华为云美国节点信息"
#   value = var.deploy_us_node ? {
#     instance_id = try(huaweicloud_compute_instance_v2.nomad_us_node[0].id, null)
#     public_ip   = try(huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4, null)
#     private_ip  = try(huaweicloud_compute_instance_v2.nomad_us_node[0].network[0].fixed_ip_v4, null)
#     datacenter  = "dc3"
#     provider    = "huawei"
#     region      = var.huawei_config.region
#     
#     # 连接信息
#     ssh_command = try("ssh ubuntu@${huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4}", null)
#     nomad_ui    = try("http://${huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4}:4646", null)
#   } : null
# }

# 集群连接信息
output "cluster_endpoints" {
  description = "集群连接端点"
  value = {
    nomad_ui_urls = compact([
      "http://100.116.158.95:4646", # dc1 - semaphore
      var.deploy_korea_node ? try("http://${oci_core_instance.nomad_kr_node[0].public_ip}:4646", null) : null, # dc2
      # var.deploy_us_node ? try("http://${huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4}:4646", null) : null # dc3 - 暂时禁用
    ])
    
    ssh_commands = compact([
      "ssh root@100.116.158.95", # dc1 - semaphore
      var.deploy_korea_node ? try("ssh ubuntu@${oci_core_instance.nomad_kr_node[0].public_ip}", null) : null, # dc2
      # var.deploy_us_node ? try("ssh ubuntu@${huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4}", null) : null # dc3 - 暂时禁用
    ])
  }
}

# Ansible inventory 生成
output "ansible_inventory" {
  description = "生成的 Ansible inventory"
  value = {
    all = {
      children = {
        nomad_servers = {
          hosts = merge(
            {
              semaphore = {
                ansible_host = "100.116.158.95"
                datacenter   = "dc1"
                provider     = "existing"
              }
            },
            var.deploy_korea_node ? {
              master = {
                ansible_host = try(oci_core_instance.nomad_kr_node[0].public_ip, "pending")
                datacenter   = "dc2"
                provider     = "oracle"
              }
            } : {}
            # var.deploy_us_node ? {
            #   ash3c = {
            #     ansible_host = try(huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4, "pending")
            #     datacenter   = "dc3"
            #     provider     = "huawei"
            #   }
            # } : {} # 暂时禁用
          )
        }
      }
    }
  }
}

# 部署后验证命令
output "verification_commands" {
  description = "部署后验证命令"
  value = [
    "# 检查集群状态",
    "nomad server members",
    "",
    "# 检查各数据中心节点",
    "nomad node status -verbose",
    "",
    "# 跨数据中心任务调度测试",
    "nomad job run examples/cross-dc-test.nomad",
    "",
    "# 访问 UI",
    join("\n", [for url in compact([
      "http://100.116.158.95:4646",
      var.deploy_korea_node ? try("http://${oci_core_instance.nomad_kr_node[0].public_ip}:4646", null) : null,
      # var.deploy_us_node ? try("http://${huaweicloud_compute_instance_v2.nomad_us_node[0].access_ip_v4}:4646", null) : null # dc3 - 暂时禁用
    ]) : "curl -s ${url}/v1/status/leader"])
  ]
}