from sqlalchemy import Column, String, Float, Integer, Text, DateTime
from sqlalchemy.sql import func
from database import Base


class Lamp(Base):
    """Marine navigation lamp catalog. Aids reference a Lamp by id; the
    battery warning/alarm thresholds for each Aid are computed from the
    lamp's voltage_min / voltage_max:
        warn  = voltage_min + (voltage_max - voltage_min) * 0.20
        alarm = voltage_min + (voltage_max - voltage_min) * 0.10
    """
    __tablename__ = "lamps"

    id              = Column(String, primary_key=True)
    manufacturer    = Column(String, nullable=False)
    model           = Column(String, nullable=False)
    lamp_count      = Column(Integer, default=1)
    voltage_min     = Column(Float, nullable=False)   # discharged threshold (V)
    voltage_max     = Column(Float, nullable=False)   # fully-charged nominal (V)
    # Battery alert thresholds as % of usable voltage range.
    # warn_pct=20 means: alert when remaining capacity ≤ 20% of (max−min).
    # Defaults match the original hardcoded values (20% / 10%).
    # Override per lamp model — e.g. Sabik recommends 30%/15%.
    warn_pct        = Column(Float, default=20.0)     # % of range → warning
    alarm_pct       = Column(Float, default=10.0)     # % of range → alarm
    notes           = Column(Text, nullable=True)
    creado_en       = Column(DateTime, server_default=func.now())
    modificado_en   = Column(DateTime, onupdate=func.now())
