from django.db import models
from django.contrib.auth.models import PermissionsMixin, AbstractBaseUser, BaseUserManager


class SiteAccountManager(BaseUserManager):
    def create_user(self, email, name, surname, phone, password):
        user = self.model(email=email, name=name, surname=surname, phone=phone, password=password)
        user.set_password(password)
        user.is_staff = False
        user.is_superuser = False
        user.save(using=self._db)
        return user

    def create_superuser(self, email, name, surname, phone, password):
        user = self.create_user(email=email, name=name, surname=surname, phone=phone, password=password)
        user.is_active = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

    def get_by_natural_key(self, email_):
        print(email_)
        return self.get(email=email_)


class SiteUser(AbstractBaseUser, PermissionsMixin):
    """
    Here we are subclassing the Django AbstractBaseUser, which comes with only
    3 fields:
    1 - password
    2 - last_login
    3 - is_active
    Note than all fields would be required unless specified otherwise, with
    `required=False` in the parentheses.
    The PermissionsMixin is a model that helps you implement permission settings
    as-is or modified to your requirements.
    More info: https://goo.gl/YNL2ax
    """
    surname = models.CharField(max_length=60, verbose_name="Фамилия")
    name = models.CharField(max_length=60, verbose_name="Имя")
    email = models.EmailField(unique=True, verbose_name="Email")
    phone = models.CharField(unique=True, max_length=16, verbose_name="Телефон")
    is_staff = models.BooleanField(default=False, verbose_name="Разрешение на вход в админку")
    REQUIRED_FIELDS = ['name', 'surname', 'phone']
    USERNAME_FIELD = 'email'

    objects = SiteAccountManager()

    def get_short_name(self):
        return self.email

    def natural_key(self):
        return self.email

    def __str__(self):
        return self.email
