from typing import TYPE_CHECKING

from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import BaseUserManager

if TYPE_CHECKING:
	from apps.accounts.models import User

class UserManager(BaseUserManager["User"]):

    def _create_user(self, email_address: str, password: str | None, **extra_fields):
        if not email_address:
            raise ValueError("The given email must be set")
        email_address = self.normalize_email(email_address)
        user: User = self.model(email_address=email_address, **extra_fields)
        user.password = make_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email_address: str, password: str | None = None, **extra_fields):
        extra_fields.setdefault("is_staff", False)
        return self._create_user(email_address, password, **extra_fields)

    def create_superuser(self, email_address: str, password: str | None = None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        if extra_fields.get("is_staff") is not True:
            raise ValueError("Superuser must have is_staff=True.")
        return self._create_user(email_address, password, **extra_fields)
