from distutils.command.upload import upload
from tokenize import blank_re
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
import uuid

from django.dispatch import receiver

# Create your models here.


class MyAccountManager(BaseUserManager):

    def create_user(self, first_name, last_name, username, email, password=None):

        if not email:
            raise ValueError('user must have email adress')
        if not username:
            raise ValueError('User must have username')

        user = self.model(

            email=self.normalize_email(email),
            username=username,
            first_name=first_name,
            last_name=last_name,

        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, first_name, last_name, username, email, password):

        user = self.create_user(

            email=self.normalize_email(email),
            username=username,
            password=password,
            first_name=first_name,
            last_name=last_name,

        )

        user.is_admin = True
        user.is_staff = True
        user.is_active = True
        user.is_superadmin = True
        user.save(using=self._db)

        return user


class Account(AbstractBaseUser):

    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    username = models.CharField(max_length=50, unique=True)
    email = models.EmailField(max_length=100, unique=True)
    phone_number = models.CharField(max_length=50)

    profile_image = models.ImageField(upload_to='photos/Account')

    date_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    is_superadmin = models.BooleanField(default=False)
    id = models.UUIDField(default=uuid.uuid4, unique=True,
                          primary_key=True, editable=False)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name']

    objects = MyAccountManager()

    def fullname(self):
        return f'{self.first_name}{self.last_name}'

    def __str__(self):

        return self.email

    def has_perm(self, perm, obj=None):

        return self.is_admin

    def has_module_perms(self, add_label):
        return True


class Message(models.Model):

    sender = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True)

    recipient = models.ForeignKey(
        Account, on_delete=models.SET_NULL, null=True, related_name="messages")

    subject = models.CharField(max_length=500)

    body = models.TextField()

    is_read = models.BooleanField(default=False, null=True)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True,
                          primary_key=True, editable=False)

    def __str__(self):
        return self.subject
