"""
Linux BenchTools - Disk SMART Data Model
"""

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


class DiskSMART(Base):
    """
    SMART health and aging data for storage devices
    """
    __tablename__ = "disk_smart_data"

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

    # Disk identification
    device_name = Column(String(50), nullable=False)  # e.g., "sda", "nvme0n1"
    model = Column(String(255), nullable=True)
    serial_number = Column(String(100), nullable=True)
    size_gb = Column(Float, nullable=True)
    disk_type = Column(String(20), nullable=True)  # "ssd" or "hdd"
    interface = Column(String(50), nullable=True)  # "sata", "nvme", "usb"

    # SMART Health Status
    health_status = Column(String(20), nullable=True)  # "PASSED", "FAILED", or null
    temperature_celsius = Column(Integer, nullable=True)

    # Aging indicators
    power_on_hours = Column(Integer, nullable=True)
    power_cycle_count = Column(Integer, nullable=True)
    reallocated_sectors = Column(Integer, nullable=True)  # Critical: bad sectors
    pending_sectors = Column(Integer, nullable=True)  # Very critical: imminent failure
    udma_crc_errors = Column(Integer, nullable=True)  # Cable/interface issues

    # SSD-specific
    wear_leveling_count = Column(Integer, nullable=True)  # 0-100 (higher is better)
    total_lbas_written = Column(Float, nullable=True)  # Total data written

    # Relationship
    hardware_snapshot = relationship("HardwareSnapshot", back_populates="disk_smart_data")

    def __repr__(self):
        return f"<DiskSMART(id={self.id}, device='{self.device_name}', health='{self.health_status}')>"
