class PostTags(db.Model):
    __tablename__ = 'post_tags'
    id = db.Column(db.Integer(), primary_key=True)
    post_id = db.Column(db.Integer(),
                        db.ForeignKey('posts.id', ondelete='CASCADE'))
    tag_id = db.Column(db.Integer(),
                       db.ForeignKey('theTags.id', ondelete='CASCADE'))

    def __repr__(self):
        return f"Role('{self.user_id}', '{self.post_id}' )"
class CommentLikes(db.Model):
    __tablename__ = 'comment_likes'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(),
                        db.ForeignKey('users.id', ondelete='CASCADE'))
    comment_id = db.Column(db.Integer(),
                           db.ForeignKey('comments.id', ondelete='CASCADE'))

    def __repr__(self):
        return f"Role('{self.user_id}', '{self.comment_id}' )"
class UserRoles(db.Model):
    __tablename__ = 'user_roles'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(),
                        db.ForeignKey('users.id', ondelete='CASCADE'))
    role_id = db.Column(db.Integer(),
                        db.ForeignKey('roles.id', ondelete='CASCADE'))

    def __repr__(self):
        return f"Role('{self.user_id}', '{self.role_id}' )"
class Portfolio(db.Model):
    __tablename__ = 'portfolios'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.Text, nullable=False)
    numberPositions = db.Column(db.Integer, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    positions = db.relationship('Position', backref='portfolio', lazy=True)

    def __repr__(self):
        return f"Portfolio('{self.name}', Size: '{self.numberPositions}')"

    @staticmethod
    def hasOneCurrency(pf):

        currency = ""

        for pos in pf.positions:
            if currency == "":
                currency = pos.currency
            else:
                if pos.currency != currency:
                    return False

        return True

    @staticmethod
    def isDefined(pf):

        balance = 0

        for pos in pf.positions:
            balance += pos.percent

        # return balance == 100

        return (abs(balance - 100) < 0.01)