import Foundation
import Virtualization

// Standalone type-check of the exact Virtualization API surface used by MacVMEngine+LinuxConfig /
// +LinuxProvision — no NucleicCore context needed, so `swiftc -typecheck` on this one file confirms the
// VZ API names/signatures compile. Not part of any build target; a verification aid (docs/LINUX_VM.md).
@available(macOS 13.0, *)
func vzApiCheck(kernel: URL, initrd: URL, rootDisk: URL, consoleLog: URL, dir: URL) throws {
    let config = VZVirtualMachineConfiguration()
    config.platform = VZGenericPlatformConfiguration()

    let bootLoader = VZLinuxBootLoader(kernelURL: kernel)
    bootLoader.initialRamdiskURL = initrd
    bootLoader.commandLine = "console=hvc0 root=/dev/vda rw"
    config.bootLoader = bootLoader

    let attachment = try VZDiskImageStorageDeviceAttachment(url: rootDisk, readOnly: false)
    config.storageDevices = [VZVirtioBlockDeviceConfiguration(attachment: attachment)]

    let net = VZVirtioNetworkDeviceConfiguration()
    net.macAddress = VZMACAddress.randomLocallyAdministered()
    net.attachment = VZNATNetworkDeviceAttachment()
    config.networkDevices = [net]

    config.socketDevices = [VZVirtioSocketDeviceConfiguration()]

    let gpu = VZVirtioGraphicsDeviceConfiguration()
    gpu.scanouts = [VZVirtioGraphicsScanoutConfiguration(widthInPixels: 1920, heightInPixels: 1200)]
    config.graphicsDevices = [gpu]

    config.keyboards = [VZUSBKeyboardConfiguration()]
    config.pointingDevices = [VZUSBScreenCoordinatePointingDeviceConfiguration()]

    let console = VZVirtioConsoleDeviceSerialPortConfiguration()
    console.attachment = try VZFileSerialPortAttachment(url: consoleLog, append: false)
    config.serialPorts = [console]

    config.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
    config.memoryBalloonDevices = [VZVirtioTraditionalMemoryBalloonDeviceConfiguration()]

    let single = VZVirtioFileSystemDeviceConfiguration(tag: "nucleicprov")
    single.share = VZSingleDirectoryShare(directory: VZSharedDirectory(url: dir, readOnly: true))
    let multi = VZVirtioFileSystemDeviceConfiguration(tag: "nucleicshare")
    multi.share = VZMultipleDirectoryShare(directories: ["w": VZSharedDirectory(url: dir, readOnly: false)])
    config.directorySharingDevices = [single, multi]

    try config.validate()
}
