#!/usr/bin/env herodo
// Snapshot Management Demo
// Demonstrates creating, listing, and deleting snapshots

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

print("╔════════════════════════════════════════════════════════════╗");
print("║  Snapshot Management Demo                                 ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

// Initialize runtime
let runtime = RuntimeBuilder()
    .ch_path("/usr/local/bin/cloud-hypervisor")
    .api_socket("/tmp/ch-snapshots.sock");

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

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

print("Creating VM...");
let handle = create_vm(vm);
let vm_id = handle.id;
print("✓ VM created: " + vm_id);
print("");

print("Starting VM...");
start_vm(handle);
print("✓ VM started");
print("");

sleep(5000);

// Create first snapshot
print("📸 Creating snapshot 1...");
snapshot(handle, "/tmp/snapshot1.snap");
print("✓ Snapshot 1 created");
print("");

sleep(2000);

// Create second snapshot
print("📸 Creating snapshot 2...");
snapshot(handle, "/tmp/snapshot2.snap");
print("✓ Snapshot 2 created");
print("");

sleep(2000);

// Create third snapshot
print("📸 Creating snapshot 3...");
snapshot(handle, "/tmp/snapshot3.snap");
print("✓ Snapshot 3 created");
print("");

// List all snapshots
print("📋 Listing all snapshots for VM " + vm_id + ":");
let snapshots = list_snapshots(vm_id);
print("  Found " + snapshots.len() + " snapshots");
print("");

// Delete a snapshot
print("🗑️  Deleting snapshot 2...");
delete_snapshot(vm_id, "snapshot2");
print("✓ Snapshot 2 deleted");
print("");

// List snapshots again
print("📋 Listing snapshots after deletion:");
let snapshots_after = list_snapshots(vm_id);
print("  Found " + snapshots_after.len() + " snapshots");
print("");

print("╔════════════════════════════════════════════════════════════╗");
print("║  Snapshot Management Demo Complete                        ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

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