from app import db
from Models import get_main_channel, PostModel, ChannelModel
from sqlalchemy.dialects.postgresql import UUID


class MainChatPostModel(PostModel):
    __tablename__ = "main_chat_post"

    id = db.Column(UUID(as_uuid=True), db.ForeignKey('post.id'), primary_key=True)

    __mapper_args__ = {
        'polymorphic_identity': "main_chat_post",
        'inherit_condition': (id == PostModel.id)
    }

    tags = db.Column(db.ARRAY(db.String), nullable=True)
    comment_channel_id = db.Column(db.ForeignKey('channel.id'), nullable=False)
    comment_channel = db.relationship(
        "ChannelModel",
        foreign_keys=comment_channel_id,
        back_populates="posts"
    )

    @property
    def comments(self):
        return len(self.comment_channel.posts)

    def __init__(self, channel_id="", channel="", **kwargs):
        self.comment_channel = ChannelModel()
        super().__init__(channel=get_main_channel(), **kwargs)
