from django.contrib.auth.base_user import BaseUserManager



class UserManager(BaseUserManager):

    use_in_migrations = True

    # -------------------------------------------------------------------------

    def __create_user(self, username, password, **extra_fields):
        if not username:
            raise ValueError("Username must be entered.")
        user = self.model(username=username, **extra_fields)
        user.set_password(password)
        user.save(using = self._db)
        return user


    def create_user(self, username, password=None, **extra_fields):
        extra_fields.setdefault("is_superuser", False)
        extra_fields.setdefault("is_staff", False)
        return self.__create_user(username, password, **extra_fields)


    def create_staffuser(self, username, password, **extra_fields):
        extra_fields.setdefault("is_superuser", False)
        extra_fields.setdefault("is_staff", True)

        if extra_fields.get("is_staff") is not True:
            raise ValueError("Staffuser must have is_staff=True.")

        return self.__create_user(username, password, **extra_fields)


    def create_superuser(self, username, password, **extra_fields):
        extra_fields.setdefault("is_superuser", True)
        extra_fields.setdefault("is_staff", True)

        if extra_fields.get("is_superuser") is not True:
            raise ValueError("Superuser must have is_superuser=True.")
        
        if extra_fields.get("is_staff") is not True:
            raise ValueError("Superuser must have is_staff=True.")

        return self.__create_user(username, password, **extra_fields)
