#!/usr/bin/env herodo
//! Cloud Hypervisor VM Creation and Lifecycle
//! Demonstrates creating and managing VMs using Cloud Hypervisor

// Example 1: Creating a basic VM
let spec = #{
    id: "test-vm",
    firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
    disk_path: "/var/lib/vms/ubuntu.qcow2",
    vcpus: 2,
    memory_mb: 2048,
    api_socket: "/tmp/test-vm.sock"
};

let vm_id = cloudhv_vm_create(spec);

// Example 2: Creating multiple VMs
let vms = [
    #{
        id: "web-server",
        firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
        disk_path: "/var/lib/vms/web.qcow2",
        vcpus: 4,
        memory_mb: 4096
    },
    #{
        id: "app-server",
        firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
        disk_path: "/var/lib/vms/app.qcow2",
        vcpus: 8,
        memory_mb: 8192
    },
    #{
        id: "db-server",
        firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
        disk_path: "/var/lib/vms/db.qcow2",
        vcpus: 16,
        memory_mb: 16384
    }
];

for vm in vms {
    let created_id = cloudhv_vm_create(vm);
}

// Example 3: VM Lifecycle - Start
cloudhv_vm_start("test-vm");

// Example 4: VM Lifecycle - Check status
let info = cloudhv_vm_info("test-vm");

if info != () {
    let status = info.runtime.status;
    let pid = info.runtime.pid;
    let log_file = info.runtime.log_file;
}

// Example 5: VM Lifecycle - Stop
cloudhv_vm_stop("test-vm", false);  // false = graceful stop

// Example 6: Forceful stop if needed
cloudhv_vm_stop("test-vm", true);   // true = force stop

// Example 7: VM Lifecycle - Delete
cloudhv_vm_delete("test-vm", false);  // false = keep disk

// Example 8: Delete VM and disk
cloudhv_vm_delete("test-vm", true);   // true = delete disk

// Example 9: Listing all VMs
let all_vms = cloudhv_vm_list();

for vm in all_vms {
    let vm_id = vm.spec.id;
    let vcpus = vm.spec.vcpus;
    let memory = vm.spec.memory_mb;
    let status = vm.runtime.status;
    let pid = vm.runtime.pid;
}

// Example 10: Creating VM with kernel boot (instead of UEFI)
let kernel_vm = #{
    id: "kernel-boot-vm",
    kernel_path: "/boot/vmlinuz",
    cmdline: "console=ttyS0 root=/dev/vda1",
    disk_path: "/var/lib/vms/kernel-vm.qcow2",
    vcpus: 2,
    memory_mb: 2048
};

cloudhv_vm_create(kernel_vm);

// Example 11: VM with extra arguments
let advanced_vm = #{
    id: "advanced-vm",
    firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
    disk_path: "/var/lib/vms/advanced.qcow2",
    vcpus: 4,
    memory_mb: 4096,
    api_socket: "/tmp/advanced-vm.sock",
    extra_args: ["--cpus", "4", "--memory", "4096M"]
};

cloudhv_vm_create(advanced_vm);

// Example 12: Complete VM workflow
let workflow_spec = #{
    id: "workflow-vm",
    firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
    disk_path: "/var/lib/vms/workflow.qcow2",
    vcpus: 2,
    memory_mb: 2048
};

// Create VM
cloudhv_vm_create(workflow_spec);

// Start VM
cloudhv_vm_start("workflow-vm");

// Check status
let workflow_info = cloudhv_vm_info("workflow-vm");
let is_running = workflow_info.runtime.status == "running";

// Stop VM
cloudhv_vm_stop("workflow-vm", false);

// Delete VM (keep disk for inspection)
cloudhv_vm_delete("workflow-vm", false);

// Example 13: Batch VM deployment
let batch_vms = [
    #{ id: "batch-1", disk_path: "/var/lib/vms/batch-1.qcow2", vcpus: 2, memory_mb: 2048 },
    #{ id: "batch-2", disk_path: "/var/lib/vms/batch-2.qcow2", vcpus: 2, memory_mb: 2048 },
    #{ id: "batch-3", disk_path: "/var/lib/vms/batch-3.qcow2", vcpus: 2, memory_mb: 2048 }
];

for batch_vm in batch_vms {
    let full_spec = #{
        id: batch_vm.id,
        firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
        disk_path: batch_vm.disk_path,
        vcpus: batch_vm.vcpus,
        memory_mb: batch_vm.memory_mb
    };
    
    cloudhv_vm_create(full_spec);
    cloudhv_vm_start(batch_vm.id);
}

// Example 14: Infrastructure as Code - VM definitions
let infrastructure = [
    #{
        role: "load-balancer",
        id: "lb-1",
        vcpus: 2,
        memory_mb: 2048,
        disk_gb: 30
    },
    #{
        role: "web-server",
        id: "web-1",
        vcpus: 4,
        memory_mb: 4096,
        disk_gb: 50
    },
    #{
        role: "api-server",
        id: "api-1",
        vcpus: 8,
        memory_mb: 8192,
        disk_gb: 100
    },
    #{
        role: "database",
        id: "db-1",
        vcpus: 16,
        memory_mb: 16384,
        disk_gb: 500
    }
];

// Deploy infrastructure
for vm_config in infrastructure {
    let spec = #{
        id: vm_config.id,
        firmware_path: "/usr/share/OVMF/CLOUDHV.fd",
        disk_path: "/var/lib/vms/" + vm_config.id + ".qcow2",
        vcpus: vm_config.vcpus,
        memory_mb: vm_config.memory_mb
    };
    
    cloudhv_vm_create(spec);
}

// Example 15: VM monitoring and management
let monitoring_list = cloudhv_vm_list();

let running_count = 0;
let stopped_count = 0;

for vm in monitoring_list {
    if vm.runtime.status == "running" {
        running_count = running_count + 1;
    } else {
        stopped_count = stopped_count + 1;
    }
}
