class Donor(db.Model):
	__tablename__ = "donor"
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(30), nullable=False)
	email = db.Column(db.String(120), unique=True, nullable=False)
	contact_no = db.Column(db.String(15), unique=True, nullable=False)
	# address = db.Column(db.String(1000), nullable=False)
	blood_group = db.Column(db.String(5), nullable=False)
	last_donation = db.Column(db.Date, nullable=False, default=func.current_date())
	bloodbanks = db.relationship("Donation", back_populates="donors")
class Bloodbank(db.Model):
	__tablename__ = "bloodbank"
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(150), nullable=False)
	address = db.Column(db.String(1000), nullable=False)
	contact_no = db.Column(db.String(20), unique=True, nullable=False)
	email = db.Column(db.String(120), nullable=False)
	latitude = db.Column(db.Float(20), nullable=False)
	longitude = db.Column(db.Float(20), nullable=False)
	geom = db.Column(Geometry('Point'), nullable=False)
	admin = db.relationship("App_user",	uselist=False, backref='bloodbank')
	donors = db.relationship("Donation", back_populates="bloodbanks")
	stats = db.relationship("Bloodbank_stats", uselist=False, backref="bloodbank")
	utilised_blood = db.relationship("Utilisation", backref="bloodbank", lazy=True)
class App_user(db.Model, UserMixin):
	__tablename__ = "app_user"
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(30), nullable=False)
	email = db.Column(db.String(120), unique=True, nullable=False)
	image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
	password = db.Column(db.String(60), nullable=False)
	contact_no = db.Column(db.String(15), unique=True, nullable=False)
	role = db.Column(db.String(15), nullable=False, default='app_user')
	bloodbank_id = db.Column(db.Integer, db.ForeignKey(Bloodbank.id))
	requests = db.relationship("Request", backref="user", lazy=True)

	def get_reset_token(self, expires_sec=1800):
		s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
		return s.dumps({'user_id': self.id}).decode('utf-8')

	@staticmethod
	def verify_reset_token(token):
		s = Serializer(current_app.config['SECRET_KEY'])
		try:
			user_id = s.loads(token)['user_id']
		except:
			return None
		return App_user.query.get(user_id)

	def __repr__(self):
		return f"User('{self.name}', '{self.email}', '{self.image_file}', '{self.contact_no}')"
class Utilisation(db.Model):
	__tablename__ = "utilisation"
	bloodbank_id = db.Column(db.Integer, db.ForeignKey(Bloodbank.id), primary_key=True)
	date_time = db.Column(db.DateTime, nullable=False, default=datetime.now(pytz.timezone('Asia/Kolkata')), primary_key=True)
	blood_group = db.Column(db.String(5), nullable=False)
	units = db.Column(db.Integer, nullable=False, default=1)
class Request(db.Model):
	__tablename__ = "request"
	user_id = db.Column(db.Integer, db.ForeignKey(App_user.id), primary_key=True)
	date = db.Column(db.DateTime, nullable=False, default=datetime.now(pytz.timezone('Asia/Kolkata')), primary_key=True)
	blood_group = db.Column(db.String(5), nullable=False)
	units = db.Column(db.Integer, nullable=False, default=1)