import getpass
from sqlalchemy import Column, Computed, Integer, String, Boolean, DateTime, Float
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 Measurement(Base):
    __tablename__ = "Measurement"

    PointID = Column(Integer, primary_key=True)
    SignalID = Column(String(36), default=lambda: str(uuid.uuid4()))
    HistorianID = Column(Integer)
    DeviceID = Column(Integer)
    PointTag = Column(String(200), nullable=False)
    AlternateTag = Column(String)
    AlternateTagIndexed = Column(String(900), Computed("CAST(LEFT(AlternateTag, 900) AS varchar(900))", persisted=True))
    SignalTypeID = Column(Integer)
    PhasorSourceIndex = Column(Integer)
    SignalReference = Column(String(200), nullable=False)
    Adder = Column(Float, default=0.0)
    Multiplier = Column(Float, default=1.0)
    Description = Column(String)
    Internal = Column(Boolean, default=True)
    Subscribed = Column(Boolean, default=False)
    Enabled = Column(Boolean, default=True)
    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)
