from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save


class FolderInfo(models.Model):
    """Adding attributes to default model user"""
    patient = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True, default="")
    pub_key = models.CharField(max_length=300, blank=True, default="")

    def __str__(self):
        """String for representing the Model object."""
        return str(self.id)


@receiver(post_save, sender=User)
def create_folderinfo(sender, instance, created, **kwargs):
    """Creates Folderinfo for user when he is created"""
    if created:
        FolderInfo.objects.create(patient=instance)


class FolderPart_Name(models.Model):
    shortcut = models.CharField(max_length=3, default="x")
    part_name = models.CharField(max_length=40, primary_key=True, default="x")

    def __str__(self):
        """String for representing the Model object."""
        return str(self.part_name)


class FolderPart(models.Model):
    part_class = models.ForeignKey(FolderPart_Name, on_delete=models.CASCADE, default="")
    folder_id = models.ForeignKey(FolderInfo, on_delete=models.CASCADE, default="")
    part_context = models.CharField(max_length=500, default="nothing for you")

    class Meta:
        unique_together = ('part_class', 'folder_id')

    def __str__(self):
        """String for representing the Model object."""
        return self.part_class.part_name


class JustifiedKey(models.Model):
    part = models.ForeignKey(FolderPart, on_delete=models.CASCADE, null=True, default="")
    justified_name = models.ForeignKey(User, on_delete=models.CASCADE, default="")
    key = models.CharField(max_length=260, default="")





