from app import db
from app.models.post import Post
from config import V5Config

class Topic(Post):
    __tablename__ = 'topic'
    __mapper_args__ = {'polymorphic_identity': __tablename__}

    # ID of the underlying [Post] object
    id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)

    # Topic title
    title = db.Column(db.Unicode(V5Config.THREAD_NAME_MAXLEN))

    # Parent forum
    forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False)
    forum    = db.relationship('Forum', backref='topics',foreign_keys=forum_id)

    # Associated thread
    thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False)
    thread    = db.relationship('Thread', foreign_keys=thread_id)

    # Number of views in the forum
    views = db.Column(db.Integer)

    def __init__(self, forum, author, title, thread):
        """
        Create a Topic.

        Arguments:
        forum  -- parent forum or sub-forum (Forum)
        author -- post author (User)
        title  -- topic title (unicode string)
        thread -- discussion thread attached to the topic (Thread)
        """

        Post.__init__(self, author)
        self.title = title
        self.views = 0
        self.thread = thread
        self.forum = forum

    def __repr__(self):
        return f'<Topic: #{self.id}>'
