from medicSearch.models import *

class Profile (models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    role = models.IntergerField(choices=ROLE_CHOICE, default=3)
    birthday = models.DataField(default=None, null=True, blank=True)
    created_at = models.DataTimeField(auto_now_add=True)
    updated_at = models.DataTimeField(auto_now=True)
    token = models.CharField(max_length=255, null=True, black=True)

    def __str__(self):
        return '{}'.format(self.user.username)
    
    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        try:
            if created:
                Profile.objects.create(user=instance)
        except:
            pass
    
    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        try:
            instance.profile.save()
        except:
            pass