"""Database models."""
import os

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class User(db.Model):
    """User account model."""
    __tablename__ = 'users'
    id  = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    email = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)
    created_on = db.Column(db.Integer, nullable=False)
    last_login= db.Column(db.Integer, nullable=False)

    def set_password(self, password):
        """Create hashed password."""
        self.UserPassword = generate_password_hash(password, method='sha256')

    def check_password(self, password):
        """Check hashed password."""
        return check_password_hash(self.UserPassword, password)

    def __repr__(self):
        return '<User {}>'.format(self.UserPassword)
        
        
class book(db.Model):
    """User account model."""
    __tablename__ = 'books'
    id  = db.Column(db.Integer, primary_key=True)
    isbn = db.Column(db.String, nullable=False)
    title= db.Column(db.String, nullable=False)
    author= db.Column(db.String, nullable=False)
    p_year= db.Column(db.String, nullable=False)
    review_id = db.Column(db.Integer, db.ForeignKey("reviewss.id"), nullable=False)
    review = db.relationship("reviews", backref="books", lazy=True)
    
    def to_json(self):
        return {
            "isbn": self.field_str,
            "title": self.field_str,
            "author": self.field_str,
            "title": self.field_str,
        }
        
class review(db.Model):
    """User account model."""
    __tablename__ = 'reviews'
    id  = db.Column(db.Integer, primary_key=True)
    reviewer = db.Column(db.String, nullable=False)
    review_date = db.Column(db.String, nullable=False)
    review = db.Column(db.String, nullable=False)
    book_id = db.Column(db.Integer, db.ForeignKey("books.id"), nullable=False)
    
    def to_json(self):
        return {
            "reviewer": self.field_str,
            "review": self.field_str,
            "author": self.field_str,
            "title": self.field_str,
        }