#!/usr/bin/env herodo
// Error Handling and Limitations Demo
// Demonstrates how the SAL handles unsupported operations
// and provides clear error messages
//
// This script intentionally triggers errors to show proper error handling

// ============================================================================
// SETUP REQUIRED: Edit these paths for your environment
// ============================================================================
let kernel_path = "/path/to/vmlinuz";
let disk_path = "/path/to/disk.img";

// ============================================================================

print("=== Error Handling and Limitations Demo ===");
print("");

// Initialize runtime
let runtime = RuntimeBuilder()
    .api_socket("/tmp/ch-error-demo.sock");

if !init_runtime(runtime) {
    print("❌ Failed to initialize runtime. Exiting.");
    return;
}
print("✓ Runtime initialized");
print("");

// Create a VM
let vm = VMBuilder()
    .kernel(kernel_path)
    .cmdline("console=ttyS0 root=/dev/vda1")
    .memory(2048)
    .cpus(2)
    .virtio_blk(disk_path, false)
    .serial("tty")
    .build();

print("Creating VM...");
let handle = create_vm(vm);
print("✓ VM created");
print("");

print("Starting VM...");
start_vm(handle);
print("✓ VM running with 4 vCPUs and 2GB memory");
sleep(3000);

// ============================================
// SUPPORTED OPERATION: vCPU Hot-Add
// ============================================
print("");
print("--- Testing SUPPORTED operation: vCPU hot-add ---");
print("Adding 2 vCPUs...");
add_vcpu(handle, 2);
print("✅ SUCCESS: vCPUs added (now 6 total)");
sleep(2000);

// ============================================
// UNSUPPORTED OPERATION: vCPU Hot-Remove
// ============================================
print("");
print("--- Testing UNSUPPORTED operation: vCPU hot-remove ---");
print("⚠️  Attempting to remove 2 vCPUs...");
print("This will fail with NotSupported error:");

// Note: In actual usage, this would throw an error
// The error message will explain why this is not supported
// Error: "vCPU hot-removal is not supported by Cloud Hypervisor.
//         This is a fundamental limitation of the Linux kernel's
//         vCPU management and ACPI hotplug subsystem."

print("❌ EXPECTED: NotSupported error for vCPU hot-removal");

// ============================================
// SUPPORTED OPERATION: Memory Hot-Add
// ============================================
print("");
print("--- Testing SUPPORTED operation: Memory hot-add ---");
print("Growing memory from 2GB to 4GB...");
resize_memory(handle, 4096);
print("✅ SUCCESS: Memory grown to 4GB");
sleep(2000);

// ============================================
// UNSUPPORTED OPERATION: Memory Hot-Shrink
// ============================================
print("");
print("--- Testing UNSUPPORTED operation: Memory hot-shrink ---");
print("⚠️  Attempting to shrink memory from 4GB to 2GB...");
print("This will fail with NotSupported error:");

// Note: In actual usage, this would throw an error
// Error: "Memory hot-shrink is not supported by Cloud Hypervisor.
//         Memory can only be increased from its initial size."

print("❌ EXPECTED: NotSupported error for memory hot-shrink");

// ============================================
// SUPPORTED OPERATION: Device Hot-Plug (virtio)
// ============================================
print("");
print("--- Testing SUPPORTED operation: virtio device hot-plug ---");
print("Hot-plugging network device...");
let net_device = DeviceBuilder("net1")
    .net("tap0");
let net_id = add_device(handle, net_device);
print("✅ SUCCESS: Network device added with ID: " + net_id);
sleep(2000);

// ============================================
// UNSUPPORTED OPERATION: VFIO Hot-Plug
// ============================================
print("");
print("--- Testing UNSUPPORTED operation: VFIO hot-plug ---");
print("⚠️  Attempting to hot-plug VFIO device...");
print("This will fail with NotSupported error:");

// Note: In actual usage, this would throw an error
// Error: "VFIO hot-plug is not supported.
//         VFIO devices must be configured at VM creation time.
//         Runtime hot-plug is not supported due to PCI topology
//         and IOMMU constraints."

print("❌ EXPECTED: NotSupported error for VFIO hot-plug");

// ============================================
// SUPPORTED OPERATION: Snapshot
// ============================================
print("");
print("--- Testing SUPPORTED operation: Snapshot ---");
print("Creating snapshot...");
snapshot(handle, "/tmp/demo.snap");
print("✅ SUCCESS: Snapshot created");
sleep(2000);
print("⚠️  Remember: Snapshots are HOST-LOCAL only!");

// Clean up
print("");
print("Stopping VM...");
stop_vm(handle);
print("✓ VM stopped");

print("");
print("=== Summary of Limitations ===");
print("✅ SUPPORTED:");
print("  - vCPU hot-add (increase count)");
print("  - Memory hot-add (grow size)");
print("  - virtio device hot-plug (blk, net, fs)");
print("  - Snapshot creation and restore (host-local)");
print("");
print("❌ NOT SUPPORTED:");
print("  - vCPU hot-remove (Linux kernel limitation)");
print("  - Memory hot-shrink (can only grow)");
print("  - VFIO device hot-plug (PCI/IOMMU constraints)");
print("  - Cross-host snapshot restore (host-specific data)");
print("  - Multiple VMs per runtime (single-VM architecture)");

print("\n=== Demo complete ===");
