import getpass
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, BigInteger
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
import uuid

Base = declarative_base()


def get_current_user_id():
    return getpass.getuser()


class Device(Base):
    __tablename__ = "Device"

    NodeID = Column(String(36), default=lambda: str(uuid.uuid4()))
    ID = Column(Integer, primary_key=True)
    ParentID = Column(Integer, ForeignKey("Device.ID"))
    UniqueID = Column(String(36), default=lambda: str(uuid.uuid4()))
    Acronym = Column(String(200), nullable=False)
    Name = Column(String(200))
    OriginalSource = Column(String(20))
    IsConcentrator = Column(Boolean, default=False)
    CompanyID = Column(Integer)
    HistorianID = Column(Integer)
    AccessID = Column(Integer, default=0)
    VendorDeviceID = Column(Integer)
    ProtocolID = Column(Integer)
    Longitude = Column(Float)
    Latitude = Column(Float)
    InterconnectionID = Column(Integer, default=1)
    ConnectionString = Column(String)
    TimeZone = Column(String(200))
    FramesPerSecond = Column(Integer, default=30)
    TimeAdjustmentTicks = Column(BigInteger, default=0)
    DataLossInterval = Column(Float, default=5.0)
    AllowedParsingExceptions = Column(Integer, default=10)
    ParsingExceptionWindow = Column(Float, default=5.0)
    DelayedConnectionInterval = Column(Float, default=5.0)
    AllowUseOfCachedConfiguration = Column(Boolean, default=True)
    AutoStartDataParsingSequence = Column(Boolean, default=True)
    SkipDisableRealTimeData = Column(Boolean, default=False)
    MeasurementReportingInterval = Column(Integer, default=100000)
    ConnectOnDemand = Column(Boolean, default=False)
    ContactList = Column(String)
    MeasuredLines = Column(Integer)
    LoadOrder = Column(Integer, default=0)
    Enabled = Column(Boolean)
    CreatedOn = Column(DateTime, default=func.now())
    CreatedBy = Column(String(200), default=get_current_user_id)
    UpdatedOn = Column(DateTime, default=func.now(), onupdate=func.now())
    UpdatedBy = Column(String(200), default=get_current_user_id, onupdate=get_current_user_id)
