'''
peewee ORM Database Model definition 
Documentation: https://docs.peewee-orm.com

please look up model.dbml for Documentation
Online Viewer: https://dbdiagram.io 

'''


from peewee import *
from playhouse.db_url import connect
from datetime import datetime

db = DatabaseProxy()

# WIP: Add Switch Function
#if False:
#    database = SqliteDatabase(None, autoconnect=False)
#else:
#    database = connect('postgresql://admin:admin@127.0.0.1:5432/learnlytics')
        
#db.initialize(database)

class BaseModel(Model):
    '''
    Base Model (needed for peewee) defines the Class Meta 
    and bounds Global db obj to every Table
    '''
    class Meta:
        database = db


class Class(BaseModel):
    '''
    Baseline Order Base 
    
    Table for Storing a Class 
    '''
    name = CharField()
    created_at = DateTimeField(default=datetime.now)


class Group(BaseModel):
    '''
    Table for Storing a project Group 
    '''
    name = CharField()
    project = CharField()
    has_passed = BooleanField()
    presentation = CharField(null = True)
    class_id = ForeignKeyField(Class, backref='class')
    created_at = DateTimeField(default=datetime.now)


class Study(BaseModel):
    '''
    Table for Storing a Study Program
    '''
    name = CharField()
    created_at = DateTimeField(default=datetime.now)


class Student(BaseModel):
    '''
    Table for Storing a Student and linking him to appropiate Tables
    '''
    prename = CharField()
    surname = CharField()
    sex = CharField()
    study = ForeignKeyField(Study, backref='study')
    class_id = ForeignKeyField(Class, backref='class')
    group_id = ForeignKeyField(Group, backref='group')
    grader = CharField()
    created_at = DateTimeField(default=datetime.now)


class Lecture(BaseModel):
    '''
    Table for defining a Lecture
    '''
    title = CharField()
    points = IntegerField()
    class_id = ForeignKeyField(Class, backref='class')
    created_at = DateTimeField(default=datetime.now)


class Submission(BaseModel):
    '''
    Table for defining a Submission from a Student for Lecture
    '''
    student_id = ForeignKeyField(Student, backref='student')
    lecture_id = ForeignKeyField(Lecture, backref='lecture')
    class_id = ForeignKeyField(Class, backref='class')
    points = FloatField()
    created_at = DateTimeField(default=datetime.now)

