# coding: utf-8
"""
class TimeseriesChangelog(Base)
===============================
"""
from sqlalchemy import Column, DateTime, ForeignKey, BigInteger, String, CHAR, text, \
                       Text, Table, Sequence
from sqlalchemy.orm import relationship
from .models_core import Timeseries
from toardb.auth_user.models import AuthUser
from toardb.base import Base

class TimeseriesChangelog(Base):
    """ Table "public.timeseries_changelog"

    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    |     Column     |           Type           | Collation | Nullable |                      Default                     |
    +================+==========================+===========+==========+==================================================+
    | datetime       | timestamp with time zone |           | not null | now()                                            |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | timeseries_id  | bigint                   |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | type_of_change | integer                  |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | description    | text                     |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | old_value      | character varying(256)   |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | new_value      | character varying(256)   |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | period_start   | timestamp with time zone |           |          |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | period_end     | timestamp with time zone |           |          |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | version        | character(28)            |           |          |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    | author_id      | integer                  |           | not null |                                                  |
    +----------------+--------------------------+-----------+----------+--------------------------------------------------+
    Indexes:
     "timeseries_changelog_pkey" PRIMARY KEY, btree (datetime)
    Foreign-key constraints:
     "timeseries_changelog_author_id_fk_auth_user_id" FOREIGN KEY (author_id) REFERENCES auth_user(id)
     "timeseries_changelog_timeseries_id_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id)
     "timeseries_changelog_type_of_change_fk_cl_vocabulary_enum_val" FOREIGN KEY (type_of_change) REFERENCES cl_vocabulary(enum_val)
    """

    __tablename__ = 'timeseries_changelog'

    datetime = Column(DateTime(True), primary_key=True, nullable=False, server_default=text("now()"))
    description = Column(Text, nullable=False)
    old_value = Column(String(256), nullable=False)
    new_value = Column(String(256), nullable=False)
    period_start = Column(DateTime(True))
    period_end = Column(DateTime(True))
    version = Column(CHAR(28))
# do not use string declaration here (not working for pytest)
# use the explicit class name here,
# see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U
    timeseries_id = Column(ForeignKey(Timeseries.id), nullable=False)
    author_id = Column(ForeignKey(AuthUser.id), nullable=False)
# still to check for some pytest solution for controlled vocabulary
    type_of_change = Column(ForeignKey('cl_vocabulary.enum_val'), nullable=False)

    author = relationship('AuthUser')
    timeseries = relationship('Timeseries')
    
#   cl_vocabulary = relationship('ClVocabulary')