"""Map inventory rows to vSphere Automation REST JSON shapes."""

from __future__ import annotations

from typing import Any

from app.vsphere.inventory import ManagedObject


def vm_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "vm": obj.moid,
        "name": obj.name,
        "power_state": props.get("power_state", "POWERED_OFF"),
        "cpu_count": props.get("cpu_count"),
        "memory_size_MiB": props.get("memory_size_mib"),
    }


def vm_info(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    cpu = dict(props.get("cpu") or {})
    return {
        "guest_OS": props.get("guest_OS"),
        "name": obj.name,
        "identity": props.get("identity"),
        "power_state": props.get("power_state"),
        "hardware": {
            "version": props.get("hardware_version"),
            "cpu": {
                "count": props.get("cpu_count"),
                "cores_per_socket": cpu.get("cores_per_socket"),
            },
            "memory": {"size_MiB": props.get("memory_size_mib")},
        },
        "nics": props.get("nics") or [],
        "disks": props.get("disks") or [],
        "boot": props.get("boot") or {},
    }


def host_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "host": obj.moid,
        "name": obj.name,
        "connection_state": props.get("connection_state", "CONNECTED"),
        "power_state": props.get("power_state", "POWERED_ON"),
    }


def host_info(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "name": obj.name,
        "connection_state": props.get("connection_state", "CONNECTED"),
        "power_state": props.get("power_state", "POWERED_ON"),
        "cpu": {
            "count": props.get("cpu_cores", 1),
            "mhz": props.get("cpu_mhz", 2000),
        },
        "memory": {"size_MiB": props.get("memory_size_mib", 8192)},
    }


def datastore_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "datastore": obj.moid,
        "name": obj.name,
        "type": props.get("type", "VMFS"),
        "free_space": props.get("free_space", 0),
        "capacity": props.get("capacity", 0),
    }


def network_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "network": obj.moid,
        "name": obj.name,
        "type": props.get("type", "STANDARD_PORTGROUP"),
    }


def datacenter_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "datacenter": obj.moid,
        "name": obj.name,
        "datastore_folder": props.get("datastore_folder"),
        "host_folder": props.get("host_folder"),
        "network_folder": props.get("network_folder"),
        "vm_folder": props.get("vm_folder"),
    }


def cluster_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "cluster": obj.moid,
        "name": obj.name,
        "drs_enabled": props.get("drs_enabled", False),
        "ha_enabled": props.get("ha_enabled", False),
    }


def folder_summary(obj: ManagedObject) -> dict[str, Any]:
    props = obj.props
    return {
        "folder": obj.moid,
        "name": obj.name,
        "type": props.get("folder_type", "VIRTUAL_MACHINE"),
    }


def resource_pool_summary(obj: ManagedObject) -> dict[str, Any]:
    return {
        "resource_pool": obj.moid,
        "name": obj.name,
    }
