# Timestamp for inventory filename during testing
variable "inventory_timestamp" {
  description = "Timestamp suffix for inventory file (e.g. 2026-06-05-143022). Leave empty for static filename."
  type        = string
  default     = ""
}

locals {
  lxc_hosts = [
    for vmid in local.vmids : {
      hostname  = module.lxc[tostring(vmid)].hostname
      ipv4      = module.lxc[tostring(vmid)].ipv4
      ipv4_host = split("/", module.lxc[tostring(vmid)].ipv4)[0]
      user      = "root"
      password  = var.password
      port      = 22
    }
  ]

  inventory_filename = var.inventory_timestamp != "" ? "inventory-lxc-${var.inventory_timestamp}.yml" : "inventory-lxc.yml"
  inventory_content  = templatefile("${path.module}/inventory-lxc.tmpl", { hosts = local.lxc_hosts })
}

# Write timestamped inventory file (archive/history)
resource "local_file" "inventory_timestamped" {
  filename = "/ansible-push/terraform-prefill/${local.inventory_filename}"
  content  = local.inventory_content
}

# Write latest inventory file (static name, always overwritten)
resource "local_file" "inventory_latest" {
  filename = "/ansible-push/terraform-prefill/inventory-lxc.yml"
  content  = local.inventory_content
}

output "inventory_path" {
  description = "Path to generated Ansible inventory file (static latest)"
  value       = local_file.inventory_latest.filename
}

output "inventory_path_archive" {
  description = "Path to generated Ansible inventory file (timestamped archive)"
  value       = local_file.inventory_timestamped.filename
}
