import random
from django.db import models
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404

# Create your models here.
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.template.defaultfilters import slugify
import uuid



# Create your models here.
USER_TYPE = (
     # admin prestataire
    ("ADMIN", "ADMIN"),  # admin
    ("DRH", "DRH"),  # Directeur des Ressources Humaines
    ("CLIENT", "CLIENT"),  # Client
    ("TECHNICIEN", "TECHNICIEN"),  # Technicien
)


class CostumUser(models.Model):

    user = models.OneToOneField(
        User, on_delete=models.CASCADE, default=None, related_name="cuser"
    )
    phone = models.CharField(max_length=45, blank=True, default="")
    avatar = models.CharField(max_length=255, blank=True, default="")
    address = models.CharField(max_length=255, blank=True, default="")
    type = models.CharField(
        max_length=45, blank=True, null=False, default=USER_TYPE[0][0], choices=USER_TYPE
    )
    image = models.ImageField(upload_to="az-images/", blank=True, null=True)

    



    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:

            if not CostumUser.objects.filter(user=instance).exists():
                CostumUser.objects.create(user=instance)

    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        pass
        # instance.costumuser.save()

    def __str__(self):
        return self.user.username
