from flask_sqlalchemy import SQLAlchemy
from app import db


#db = SQLAlchemy()

 
class User(db.Model):
    """An admin user capable of viewing reports.

    :param str email: email address of user
    :param str password: encrypted password for the user

    """
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(80), unique=True, nullable=False)
 
    #password = db.Column(db.String)
    photo = db.Column(db.String)
    authenticated = db.Column(db.Boolean, default=False)
    confidence = db.Column(db.Integer, default=False)

    exams = db.relationship('Enrolment', backref='user', lazy=True)

    role = db.Column(db.String(5))
    timing = db.Column(db.String(6), default=0)
    faceid = db.Column(db.String(80))
    deleted = db.Column(db.Boolean, default=False)
    errors = db.Column(db.Integer, default=0)
    

    def is_active(self):
        """True, as all users are active."""
        return True

    def get_id(self):
 
        return self.id

    def is_authenticated(self):
        """Return True if the user is authenticated."""
        return self.authenticated

    def is_anonymous(self):
        """False, as anonymous users aren't supported."""
        return False



 

class Photos(db.Model):
 
    __tablename__ = 'photos'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    filename = db.Column(db.String(80), unique=True, nullable=False)

class Exams(db.Model):
 
    __tablename__ = 'exams'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=True, nullable=False)

class PassedExams(db.Model):
 
    __tablename__ = 'enrolnment'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), unique=True, nullable=False)

class Question(db.Model):
 
    __tablename__ = 'question'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), nullable=True)
    option1 = db.Column(db.String(80), nullable=True)
    option2 = db.Column(db.String(80), nullable=False)
    option3 = db.Column(db.String(80), nullable=False)
    option4 = db.Column(db.String(80), nullable=False)
    option5 = db.Column(db.String(80), nullable=False)
    exam_id = db.Column(db.Integer, db.ForeignKey('exam.id'), nullable=False)

 
class Enrolment(db.Model):
 
    __tablename__ = 'enrolment'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    exam_id = db.Column(db.Integer, db.ForeignKey('exams.id'), nullable=False)
    date = db.Column(db.DateTime, nullable=False)
    status = db.Column(db.String(50), nullable=False)


class Answers(db.Model):
 
    __tablename__ = 'user_answers'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False)
    answer = db.Column(db.Integer, nullable=True)
 