locals {
  vm_offset  = tonumber(var.run_id) % 44 # 211 - 254
  clone_vmid = 9100 + local.vm_offset
  clone_ip   = "192.168.178.${211 + local.vm_offset}/24"
}

resource "proxmox_virtual_environment_vm" "e2e_clone" {
  name        = "e2e-run-${var.run_id}"
  description = "Ephemeral E2E test VM for run ${var.run_id}"
  tags        = ["opentofu", "e2e", "ephemeral", "clone"]

  node_name = var.node_name
  vm_id     = local.clone_vmid

  clone {
    vm_id = var.clone_template_id
    full  = false
  }

  cpu {
    cores = 4
    type  = "host"
  }

  memory {
    dedicated = 8192
  }

  network_device {
    bridge = "vmbr0"
  }

  agent {
    enabled = false
  }
  stop_on_destroy = true

  initialization {
    ip_config {
      ipv4 {
        address = local.clone_ip
        gateway = var.gateway
      }
    }

    user_account {
      username = "debian"
      password = var.clone_vm_password
    }
  }
}

output "clone_vmid" {
  description = "VMID of the cloned E2E VM"
  value       = local.clone_vmid
}

output "clone_ip" {
  description = "IP address of the cloned E2E VM (without CIDR)"
  value       = "192.168.178.${211 + local.vm_offset}"  # 211-254
}
