"""
Linux BenchTools - Hardware Snapshot Model
"""

from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, Text, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from app.db.base import Base


class HardwareSnapshot(Base):
    """
    Hardware configuration snapshot at the time of a benchmark
    """
    __tablename__ = "hardware_snapshots"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    device_id = Column(Integer, ForeignKey("devices.id"), nullable=False, index=True)
    captured_at = Column(DateTime, nullable=False, default=datetime.utcnow)

    # CPU
    cpu_vendor = Column(String(100), nullable=True)
    cpu_model = Column(String(255), nullable=True)
    cpu_microarchitecture = Column(String(100), nullable=True)
    cpu_cores = Column(Integer, nullable=True)
    cpu_threads = Column(Integer, nullable=True)
    cpu_base_freq_ghz = Column(Float, nullable=True)
    cpu_max_freq_ghz = Column(Float, nullable=True)
    cpu_cache_l1_kb = Column(Integer, nullable=True)
    cpu_cache_l2_kb = Column(Integer, nullable=True)
    cpu_cache_l3_kb = Column(Integer, nullable=True)
    cpu_flags = Column(Text, nullable=True)  # JSON array
    cpu_tdp_w = Column(Float, nullable=True)

    # RAM
    ram_total_mb = Column(Integer, nullable=True)
    ram_used_mb = Column(Integer, nullable=True)  # NEW: RAM utilisée
    ram_free_mb = Column(Integer, nullable=True)  # NEW: RAM libre
    ram_shared_mb = Column(Integer, nullable=True)  # NEW: RAM partagée (tmpfs/vidéo)
    ram_slots_total = Column(Integer, nullable=True)
    ram_slots_used = Column(Integer, nullable=True)
    ram_ecc = Column(Boolean, nullable=True)
    ram_layout_json = Column(Text, nullable=True)  # JSON array

    # GPU
    gpu_summary = Column(Text, nullable=True)
    gpu_vendor = Column(String(100), nullable=True)
    gpu_model = Column(String(255), nullable=True)
    gpu_driver_version = Column(String(100), nullable=True)
    gpu_memory_dedicated_mb = Column(Integer, nullable=True)
    gpu_memory_shared_mb = Column(Integer, nullable=True)
    gpu_api_support = Column(Text, nullable=True)

    # Storage
    storage_summary = Column(Text, nullable=True)
    storage_devices_json = Column(Text, nullable=True)  # JSON array
    partitions_json = Column(Text, nullable=True)  # JSON array

    # Network
    network_interfaces_json = Column(Text, nullable=True)  # JSON array
    network_shares_json = Column(Text, nullable=True)  # JSON array

    # OS / Motherboard
    os_name = Column(String(100), nullable=True)
    os_version = Column(String(100), nullable=True)
    kernel_version = Column(String(100), nullable=True)
    architecture = Column(String(50), nullable=True)
    virtualization_type = Column(String(50), nullable=True)
    screen_resolution = Column(String(50), nullable=True)
    display_server = Column(String(50), nullable=True)
    session_type = Column(String(50), nullable=True)
    last_boot_time = Column(String(50), nullable=True)
    uptime_seconds = Column(Float, nullable=True)
    battery_percentage = Column(Float, nullable=True)
    battery_status = Column(String(50), nullable=True)
    battery_health = Column(String(50), nullable=True)
    motherboard_vendor = Column(String(100), nullable=True)
    motherboard_model = Column(String(255), nullable=True)
    bios_vendor = Column(String(100), nullable=True)
    bios_version = Column(String(100), nullable=True)
    bios_date = Column(String(50), nullable=True)
    hostname = Column(String(255), nullable=True)
    desktop_environment = Column(String(100), nullable=True)

    # PCI and USB Devices
    pci_devices_json = Column(Text, nullable=True)  # JSON array
    usb_devices_json = Column(Text, nullable=True)  # JSON array

    # Misc
    sensors_json = Column(Text, nullable=True)  # JSON object
    raw_info_json = Column(Text, nullable=True)  # JSON object

    # Relationships
    device = relationship("Device", back_populates="hardware_snapshots")
    benchmarks = relationship("Benchmark", back_populates="hardware_snapshot")
    disk_smart_data = relationship("DiskSMART", back_populates="hardware_snapshot", cascade="all, delete-orphan")

    def __repr__(self):
        return f"<HardwareSnapshot(id={self.id}, device_id={self.device_id}, captured_at='{self.captured_at}')>"
