# Virtual Machine CLI Commands
#
# User-facing commands for VM management.
# Part of Phase 1: VM Lifecycle Foundation

use lib_provisioning/vm/ {
    "vm-create"
    "vm-start"
    "vm-stop"
    "vm-delete"
    "vm-list"
    "vm-info"
    "vm-ssh"
    "vm-scp-to"
    "vm-scp-from"
    "vm-exec"
}

export def "vm create" [
    name: string                  # VM name
    --base-image: string          # Base OS image
    --cpu: int = 2                # CPU cores
    --memory: int = 4096          # Memory in MB
    --disk: int = 20              # Disk in GB
    --permanent = false           # Keep after restart
    --temporary = false           # Auto-cleanup
    --auto-cleanup-hours: int = 24 # TTL if temporary
    --taskserv: string            # Single taskserv to install
    --network: string = "bridge"  # Network mode
]: table {
    """
    Create a virtual machine.

    Examples:
        # Temporary test VM
        provisioning vm create test-k8s \\
            --temporary \\
            --cpu 4 --memory 8192

        # Permanent dev VM
        provisioning vm create dev-rust \\
            --permanent \\
            --cpu 8 --memory 16384 --disk 50 \\
            --taskserv rust

        # With custom image
        provisioning vm create test \\
            --base-image ubuntu-22.04 \\
            --temporary
    """

    let config = {
        name: $name
        base_image: ($base_image // "ubuntu-22.04")
        cpu: $cpu
        memory_mb: $memory
        disk_gb: $disk
        permanent: $permanent
        temporary: $temporary
        auto_cleanup: $temporary
        auto_cleanup_hours: $auto_cleanup_hours
        taskserv: $taskserv
        network_mode: $network
        backend: "libvirt"
    }

    let result = (vm-create $config)

    if $result.success {
        print $"✓ VM '($name)' created successfully"
        print $"  ID: ($result.vm_id)"
        print $"  Disk: ($result.disk_path)"
    } else {
        print $"✗ Failed to create VM: ($result.error)"
    }

    [$result]
}

export def "vm list" [
    --permanent = false           # List only permanent
    --temporary = false           # List only temporary
    --running = false             # List only running
]: table {
    """
    List virtual machines.

    Examples:
        # List all VMs
        provisioning vm list

        # List permanent VMs
        provisioning vm list --permanent

        # List running VMs
        provisioning vm list --running
    """

    vm-list --permanent=$permanent --temporary=$temporary --running=$running
}

export def "vm start" [
    name: string                  # VM name
]: table {
    """
    Start a virtual machine.

    Examples:
        provisioning vm start dev-rust
    """

    let result = (vm-start $name)

    if $result.success {
        print $"✓ VM '$name' started"
    } else {
        print $"✗ Failed to start VM: ($result.error)"
    }

    [$result]
}

export def "vm stop" [
    name: string                  # VM name
    --force = false               # Force shutdown
]: table {
    """
    Stop a virtual machine.

    Examples:
        provisioning vm stop dev-rust
        provisioning vm stop test --force
    """

    let result = (vm-stop $name --force=$force)

    if $result.success {
        print $"✓ VM '$name' stopped"
    } else {
        print $"✗ Failed to stop VM: ($result.error)"
    }

    [$result]
}

export def "vm delete" [
    name: string                  # VM name
    --force = false               # Force deletion
]: table {
    """
    Delete a virtual machine.

    Examples:
        provisioning vm delete test-k8s
    """

    let result = (vm-delete $name --force=$force)

    if $result.success {
        print $"✓ VM '$name' deleted"
    } else {
        print $"✗ Failed to delete VM: ($result.error)"
    }

    [$result]
}

export def "vm info" [
    name: string                  # VM name
]: record {
    """
    Get VM information.

    Examples:
        provisioning vm info dev-rust
    """

    vm-info $name
}

export def "vm ssh" [
    name: string                  # VM name
    --command: string             # Command to execute
]: table {
    """
    SSH into VM or execute command.

    Examples:
        # Interactive shell
        provisioning vm ssh dev-rust

        # Execute command
        provisioning vm ssh dev-rust --command "uname -a"

        # Run remote script
        provisioning vm ssh test --command "bash /path/to/script.sh"
    """

    let result = (vm-ssh $name --command=$command)

    if not $result.success {
        print $"✗ SSH failed: ($result.error)"
    }

    [$result]
}

export def "vm exec" [
    name: string                  # VM name
    command: string               # Command to execute
]: table {
    """
    Execute command in VM.

    Examples:
        provisioning vm exec dev-rust "cargo --version"
        provisioning vm exec test "kubectl get nodes"
    """

    let result = (vm-exec $name $command)

    if $result.success {
        print $result.output
    } else {
        print $"✗ Command failed: ($result.error)"
    }

    [$result]
}

export def "vm scp" [
    direction: string             # "to" or "from"
    vm_name: string               # VM name
    src: string                   # Source path
    dst: string                   # Destination path
]: table {
    """
    Copy files to/from VM.

    Examples:
        # Copy to VM
        provisioning vm scp to dev-rust /local/file /remote/path

        # Copy from VM
        provisioning vm scp from dev-rust /remote/file /local/path
    """

    let result = (
        if $direction == "to" {
            vm-scp-to $vm_name $src $dst
        } else if $direction == "from" {
            vm-scp-from $vm_name $src $dst
        } else {
            {success: false, error: "Direction must be 'to' or 'from'"}
        }
    )

    if $result.success {
        print $"✓ ($result.message)"
    } else {
        print $"✗ Copy failed: ($result.error)"
    }

    [$result]
}
