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




class Role(models.Model):
    role = models.CharField(max_length=20)
    role_desc = models.TextField()

    def __str__(self):
        return self.role

class BaseUser(AbstractUser):
    username = None
    image = models.ImageField(null=True, blank=True)
    bio = models.TextField(null=True, blank=True)
    address = models.CharField(null=True, blank=True, max_length=50)
    contact_number = models.CharField(unique=True, max_length=15, null=True, blank=True)
    email = models.EmailField(unique=True)
    citizenship = models.FileField(upload_to='uploads/citizenship', null=True, blank=True)
    certificate = models.FileField(upload_to='uploads/certificates', blank=True, null=True)
    #is_activated = models.BooleanField(default=False)
    token = models.CharField(max_length=64,blank=True,null=True)
    is_verified = models.BooleanField(default=False)
    price = models.DecimalField(max_digits=9999, decimal_places=2, null=True, blank=True)
    ratings = models.IntegerField(default=1)
    role = models.ForeignKey('Role', on_delete=models.SET_NULL, related_name="users", null=True, blank=True)
    member_since = models.DateTimeField(auto_now_add=True)
    profile_updated = models.DateTimeField(auto_now=True)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["image", "contact_number", "bio", "address", "citizenship", "certificate", "price", "password"]

    def __str__(self):
        return self.email
    
    @receiver(post_save, sender=AbstractUser)
    def user_to_inactive(sender, instance, created, update_fields, **kwargs):
        if created:
            instance.is_active = False
