"""
Linux BenchTools - Hardware Schemas
"""

from pydantic import BaseModel, ConfigDict, Field
from typing import Optional, List


class CPUInfo(BaseModel):
    """CPU information schema"""
    vendor: Optional[str] = None
    model: Optional[str] = None
    microarchitecture: Optional[str] = None
    cores: Optional[int] = None
    threads: Optional[int] = None
    base_freq_ghz: Optional[float] = None
    max_freq_ghz: Optional[float] = None
    cache_l1_kb: Optional[int] = None
    cache_l2_kb: Optional[int] = None
    cache_l3_kb: Optional[int] = None
    flags: Optional[List[str]] = None
    tdp_w: Optional[float] = None


class RAMSlot(BaseModel):
    """RAM slot information"""
    slot: str
    size_mb: int
    type: Optional[str] = None
    speed_mhz: Optional[int] = None
    vendor: Optional[str] = None
    part_number: Optional[str] = None


class RAMInfo(BaseModel):
    """RAM information schema"""
    total_mb: int
    used_mb: Optional[int] = None  # NEW
    free_mb: Optional[int] = None  # NEW
    shared_mb: Optional[int] = None  # NEW
    slots_total: Optional[int] = None
    slots_used: Optional[int] = None
    ecc: Optional[bool] = None
    layout: Optional[List[RAMSlot]] = None


class GPUInfo(BaseModel):
    """GPU information schema"""
    vendor: Optional[str] = None
    model: Optional[str] = None
    driver_version: Optional[str] = None
    memory_dedicated_mb: Optional[int] = None
    memory_shared_mb: Optional[int] = None
    api_support: Optional[List[str]] = None


class StorageDevice(BaseModel):
    """Storage device information"""
    name: str
    type: Optional[str] = None
    interface: Optional[str] = None
    capacity_gb: Optional[float] = None  # Changed from int to float
    vendor: Optional[str] = None
    model: Optional[str] = None
    smart_health: Optional[str] = None
    temperature_c: Optional[int] = None


class Partition(BaseModel):
    """Partition information"""
    name: str
    mount_point: Optional[str] = None
    fs_type: Optional[str] = None
    used_gb: Optional[float] = None
    total_gb: Optional[float] = None
    free_gb: Optional[float] = None


class StorageInfo(BaseModel):
    """Storage information schema"""
    devices: Optional[List[StorageDevice]] = None
    partitions: Optional[List[Partition]] = None


class NetworkInterface(BaseModel):
    """Network interface information"""
    name: str
    type: Optional[str] = None
    mac: Optional[str] = None
    ip: Optional[str] = None
    speed_mbps: Optional[int] = None
    driver: Optional[str] = None
    ssid: Optional[str] = None
    wake_on_lan: Optional[bool] = None


class NetworkInfo(BaseModel):
    """Network information schema"""
    interfaces: Optional[List[NetworkInterface]] = None


class NetworkShare(BaseModel):
    """Mounted network share information"""
    protocol: Optional[str] = None
    source: Optional[str] = None
    mount_point: Optional[str] = None
    fs_type: Optional[str] = None
    options: Optional[str] = None
    total_gb: Optional[float] = None
    used_gb: Optional[float] = None
    free_gb: Optional[float] = None


class MotherboardInfo(BaseModel):
    """Motherboard information schema"""
    vendor: Optional[str] = None
    model: Optional[str] = None
    bios_vendor: Optional[str] = None
    bios_version: Optional[str] = None
    bios_date: Optional[str] = None


class OSInfo(BaseModel):
    """Operating system information schema"""
    name: Optional[str] = None
    version: Optional[str] = None
    kernel_version: Optional[str] = None
    architecture: Optional[str] = None
    virtualization_type: Optional[str] = None
    hostname: Optional[str] = None
    desktop_environment: Optional[str] = None
    session_type: Optional[str] = None
    display_server: Optional[str] = None
    screen_resolution: Optional[str] = None
    last_boot_time: Optional[str] = None
    uptime_seconds: Optional[float] = None
    battery_percentage: Optional[float] = None
    battery_status: Optional[str] = None
    battery_health: Optional[str] = None


class PCIDevice(BaseModel):
    """PCI device information"""
    model_config = ConfigDict(populate_by_name=True)
    slot: str
    class_: Optional[str] = Field(default=None, alias="class")
    vendor: Optional[str] = None
    device: Optional[str] = None


class USBDevice(BaseModel):
    """USB device information"""
    bus: str
    device: str
    vendor_id: Optional[str] = None
    product_id: Optional[str] = None
    name: Optional[str] = None


class SensorsInfo(BaseModel):
    """Sensors information schema"""
    cpu_temp_c: Optional[float] = None
    disk_temps_c: Optional[dict] = None  # {"/dev/nvme0n1": 42}


class RawInfo(BaseModel):
    """Raw command output"""
    lscpu: Optional[str] = None
    lsblk: Optional[str] = None
    dmidecode: Optional[str] = None


class HardwareData(BaseModel):
    """Complete hardware information payload"""
    cpu: Optional[CPUInfo] = None
    ram: Optional[RAMInfo] = None
    gpu: Optional[GPUInfo] = None
    storage: Optional[StorageInfo] = None
    network: Optional[NetworkInfo] = None
    network_shares: Optional[List[NetworkShare]] = None
    motherboard: Optional[MotherboardInfo] = None
    os: Optional[OSInfo] = None
    sensors: Optional[SensorsInfo] = None
    raw_info: Optional[RawInfo] = None
    pci_devices: Optional[List[PCIDevice]] = None
    usb_devices: Optional[List[USBDevice]] = None


class HardwareSnapshotResponse(BaseModel):
    """Hardware snapshot response"""
    id: int
    device_id: int
    captured_at: str

    # CPU
    cpu_vendor: Optional[str] = None
    cpu_model: Optional[str] = None
    cpu_cores: Optional[int] = None
    cpu_threads: Optional[int] = None
    cpu_base_freq_ghz: Optional[float] = None
    cpu_max_freq_ghz: Optional[float] = None

    # RAM
    ram_total_mb: Optional[int] = None
    ram_used_mb: Optional[int] = None
    ram_free_mb: Optional[int] = None
    ram_shared_mb: Optional[int] = None
    ram_slots_total: Optional[int] = None
    ram_slots_used: Optional[int] = None

    # GPU
    gpu_summary: Optional[str] = None
    gpu_model: Optional[str] = None

    # Storage
    storage_summary: Optional[str] = None
    storage_devices_json: Optional[str] = None
    partitions_json: Optional[str] = None

    # Network
    network_interfaces_json: Optional[str] = None
    network_shares_json: Optional[str] = None

    # OS / Motherboard / BIOS
    os_name: Optional[str] = None
    os_version: Optional[str] = None
    kernel_version: Optional[str] = None
    architecture: Optional[str] = None
    virtualization_type: Optional[str] = None
    hostname: Optional[str] = None
    desktop_environment: Optional[str] = None
    screen_resolution: Optional[str] = None
    display_server: Optional[str] = None
    session_type: Optional[str] = None
    last_boot_time: Optional[str] = None
    uptime_seconds: Optional[float] = None
    battery_percentage: Optional[float] = None
    battery_status: Optional[str] = None
    battery_health: Optional[str] = None
    motherboard_vendor: Optional[str] = None
    motherboard_model: Optional[str] = None
    bios_vendor: Optional[str] = None
    bios_version: Optional[str] = None
    bios_date: Optional[str] = None

    # PCI and USB Devices
    pci_devices_json: Optional[str] = None
    usb_devices_json: Optional[str] = None

    class Config:
        from_attributes = True
