from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

class User(db.Model):
	__tablename__= 'users'
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(100), default="")
	email = db.Column(db.String(120),nullable=False, unique=True)
	password = db.Column(db.String(120), nullable=False)
	avatar = db.Column(db.String(100), default="sin_foto.png")
	active = db.Column(db.Boolean,default=True) 
	certificates = db.relationship("Certificate", backref="user", lazy=True)
 
	def serialize(self):
		return{
			"id":self.id,
			"name":self.name,	
			"email":self.email,
			"avatar":self.avatar,
			"active":self.active
		}

	def save(self):
		db.session.add(self)
		db.session.commit()

	def update(self):
		db.session.commit()	


	def delete(self):
		db.session.delete(self)
		db.session.commit()


class Certificate(db.Model):
	__tablename__='certificates'
	id = db.Column(db.Integer, primary_key= True)
	title = db.Column(db.String(100), nullable=False)	
	document = db.Column(db.String(100), nullable=False)
	date = db.Column(db.DateTime, nullable=False)
	user_id = db.Column(db.Integer,db.ForeignKey("users.id"), nullable=False)


	def serialize(self):
		return{
			"id":self.id,
			"title":self.title,	
			"document":self.document,
			"date":self.date,
			"user":{
				"id":self.user.id,
				"name":self.user.name
			}
		}