"""Runnable deterministic demo for VM lifecycle tools."""

from __future__ import annotations

import json
from typing import Any

from pyro_mcp.api import Pyro
from pyro_mcp.vm_manager import DEFAULT_MEM_MIB, DEFAULT_TTL_SECONDS, DEFAULT_VCPU_COUNT

INTERNET_PROBE_COMMAND = (
    'python3 -c "import urllib.request; '
    "print(urllib.request.urlopen('https://example.com', timeout=10).status)"
    '"'
)


def _demo_command(status: dict[str, Any]) -> str:
    if bool(status.get("network_enabled")) and status.get("execution_mode") == "guest_vsock":
        return INTERNET_PROBE_COMMAND
    return "git --version"


def run_demo(*, network: bool = False) -> dict[str, Any]:
    """Create/start/exec/delete a VM and return command output."""
    pyro = Pyro()
    status = {
        "network_enabled": network,
        "execution_mode": "guest_vsock" if network else "host_compat",
    }
    return pyro.run_in_vm(
        environment="debian:12",
        command=_demo_command(status),
        vcpu_count=DEFAULT_VCPU_COUNT,
        mem_mib=DEFAULT_MEM_MIB,
        timeout_seconds=30,
        ttl_seconds=DEFAULT_TTL_SECONDS,
        network=network,
    )


def main() -> None:
    """Run the deterministic lifecycle demo."""
    print(json.dumps(run_demo(), indent=2, sort_keys=True))
