import datetime

from django.db import models
from django.utils import timezone

#from django.contrib.postgres.fields import JSONField

from django.contrib.auth.models import User


# Create your models here.
class Slide(models.Model):
    title = models.CharField(max_length=200, default='', null=True, blank=True)
    title_short = models.CharField(max_length=200, default='', null=True, blank=True)
    description = models.CharField(max_length=200, default='', null=True, blank=True)
    slide_type = models.CharField(max_length=200, default='text/html')
    icon = models.CharField(max_length=200, null=True, blank=True)
    content = models.TextField(default='', null=True, blank=True)
    solution = models.TextField(default='', null=True, blank=True)
    grading = models.BooleanField(default=False)
    #data = JSONField()
    data = models.TextField(default='{}', null=True, blank=True)
    source_path = models.CharField(max_length=500, default='', null=True, blank=True)

    class Meta:
        ordering = ['source_path']

    def __str__(self):
        return self.title




class Section(models.Model):
    #course = models.ForeignKey('Course') # many to many, on_delete=models.CASCADE?
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    section_type = models.CharField(max_length=200, default='text/html')
    source_path = models.CharField(max_length=500, default='', null=True, blank=True)

    slides = models.ManyToManyField(Slide)

    class Meta:
        ordering = ['source_path']


    def __str__(self):
        return self.title



class Course(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    course_type = models.CharField(max_length=200, default='text/html')
    source_path = models.CharField(max_length=500, default='', null=True, blank=True)

    sections = models.ManyToManyField(Section)



    def __str__(self):
        return self.title





# User stuff reliated to courses
class UserCourse(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    progress_section = models.ForeignKey(Section, on_delete=models.CASCADE)
    progress_slide = models.ForeignKey(Slide, on_delete=models.CASCADE)


class UserSolution(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    slide = models.ForeignKey(Slide, on_delete=models.CASCADE)
    source_path = models.CharField(max_length=500, default='', null=True, blank=True)

    data = models.TextField(default='{}', null=True, blank=True)

    date = models.DateTimeField('date submitted')
    passed = models.BooleanField()
    tries = models.PositiveSmallIntegerField()
    duration = models.DurationField()


class UserEvent(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField('timestamp')
    event_type = models.TextField()
    event_data = models.TextField()

    #course = models.ForeignKey('Course') # many to many, on_delete=models.CASCADE?
