from sqlalchemy import Column, String, ForeignKey, Integer
from sqlalchemy.dialects.mysql import BIT
from sqlalchemy.orm import relationship

from .base import Base, BaseMixin


class MetaDataTable(Base, BaseMixin):
    __tablename__ = 'meta_data_table'
    table_name = Column(String(255), unique=True)
    table_columns = relationship("MetaDataColumn")

    def __repr__(self):
        return f'MetaDataTable({self.id} {self.table_name})'

    def __str__(self):
        return f'{self.table_name}({self.id})'


class MetaDataColumn(Base, BaseMixin):
    __tablename__ = 'meta_data_column'
    column_name = Column(String(255), nullable=False)
    column_sync_name = Column(String(255))
    column_type = Column(String(255))
    column_modifier = Column(String(255), nullable=True)
    column_order = Column(Integer)
    table_id = Column(String, ForeignKey('meta_data_table.id'))
    table = relationship("MetaDataTable", back_populates="table_columns")
    column_label = Column(String(255))
    filter_label = Column(String(255))
    is_shown = Column(BIT(1))
    show_filter = Column(BIT(1))
    ref_column = Column(String, nullable=True)

    def __repr__(self):
        if self.column_name is None:
            return f'MetaDataColumn({self.id} {self.table.table_name}.__)'
        else:
            return f'MetaDataColumn({self.id} {self.table.table_name}.{self.column_name})'

    def __str__(self):
        return f'{self.column_name}({self.id})'
