resource "proxmox_virtual_environment_download_file" "debian_13" {
  content_type          = "iso"
  datastore_id          = "local"
  file_name             = "debian-13-generic-amd64.img"
  node_name             = var.pve_node
  url                   = var.debian_image_url
  checksum              = var.debian_image_checksum
  checksum_algorithm    = var.debian_image_checksum_algorithm
  overwrite             = true
  overwrite_unmanaged   = true
}

resource "proxmox_virtual_environment_file" "user_config" {
  content_type  = "snippets"
  datastore_id  = "local"
  node_name     = var.pve_node

  source_raw {
    data        = file("cloud-init/user-config.yml")
    file_name   = "user-config.yml"
  }
}

resource "proxmox_virtual_environment_file" "vendor_config" {
  content_type  = "snippets"
  datastore_id  = "local"
  node_name     = var.pve_node

  source_raw {
    data        = file("cloud-init/vendor-config.yml")
    file_name   = "vendor-config.yml"
  }
}

resource "proxmox_virtual_environment_vm" "debian_13_test" {
    depends_on          = [ 
        proxmox_virtual_environment_file.user_config,
        proxmox_virtual_environment_file.vendor_config 
    ]
    name                = "debian-13"
    description         = "Debian 13 created with Terraform"
    tags                = ["terraform", "debian"]
    node_name           = var.pve_node

    cpu {
        cores   = 2
    }
    memory {
        dedicated = 2048
        floating  = 2048
    }

      disk {
        datastore_id    = "local-zfs"
        file_id         = proxmox_virtual_environment_download_file.debian_13.id
        interface       = "virtio0"
        iothread        = true
        discard         = "on"
        ssd             = true
    # disk {
    # datastore_id = var.node_datastore # Datastore to hold the disk
    # interface    = "scsi0"             # Primary disk interface
    # size         = 4                   # Disk size in GB
     }

  network_device {
        bridge          = "vmbr0"
        model           = "virtio"
    }
    operating_system {
        type            = "l26"
    }
    agent {
        enabled         = true
    }
    initialization {
    datastore_id = var.node_datastore # Datastore to hold the disk
      ip_config {
            ipv4 {
                address     = "dhcp"
            }
        }
        # Link cloud-init yml files
        user_data_file_id   = proxmox_virtual_environment_file.user_config.id
        vendor_data_file_id = proxmox_virtual_environment_file.vendor_config.id
    }
}

output "debian_13_test_ip_address" {
    value = proxmox_virtual_environment_vm.debian_13_test.ipv4_addresses
}
