import enum

from sqlalchemy import (
    Column,
    DateTime,
    Enum,
    ForeignKey,
    Integer,
    String,
    UniqueConstraint,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

Base = declarative_base()


class NetObjectType(enum.Enum):
    AS = enum.auto()
    IX = enum.auto()


class NetObject(Base):
    __tablename__ = "net_objects"
    __table_args__ = (
        UniqueConstraint(
            "type", "name", "number", name="net_objects_type_name_number_unique"
        ),
    )

    id = Column(Integer, primary_key=True)
    type = Column(Enum(NetObjectType), nullable=False)
    name = Column(String, nullable=False)
    number = Column(Integer, nullable=False)

    counts = relationship("Count", back_populates="object")


class Count(Base):
    __tablename__ = "counts"
    __table_args__ = (
        UniqueConstraint("object_id", "time", name="counts_object_id_time_unique"),
    )

    object_id = Column(Integer, ForeignKey("net_objects.id"), primary_key=True)
    time = Column(DateTime, primary_key=True)
    value = Column(Integer, nullable=False)

    object = relationship("NetObject", back_populates="counts")
