from locale import normalize
from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """

    def create_user(self, username, email, first_name, last_name, password=None):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError("email is required")
        email = self.normalize_email(email)
        user = self.model(username=username, email=email,
                          first_name=first_name, last_name=last_name)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, first_name, last_name, password=None):
        """
        Create and save a SuperUser with the given email and password.
        """
        user = self.create_user(
            username, email, first_name, last_name
        )
        user.is_admin = True
        user.save(using=self._db)
        return user
