from api.utils.database import db
from marshmallow_sqlalchemy import ModelSchema
from marshmallow import fields
from api.models.links import LinkSchema

class User(db.Model):
    __tablemane__= 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)
    created = db.Column(db.DateTime, server_default=db.func.now())
    #links = db.relationship('Link', backref='User', cascade="all, delete-orphan")

    def __init__(self, username, links):
        self.username = username
        self.links = links

    def create(self):
        db.session.add(self)
        db.session.commit()
        return self


@classmethod
def find_by_username(cls, username):
    return cls.query.filter_by(username=username).first()


@staticmethod
def generate_hash(password):
    return sha256.hash(password)


@staticmethod
def verify_hash(password, hash):
    return sha256.verify(password, hash)


class UserSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = User
        sqla_session = db.session

    id = fields.Number(dump_only=True)
    username = fields.String(required=True)
    email = fields.String(required=True)