from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
    use_in_migrations = True

    def get_organization(self, email):
        from invitations.models import Invitation
        from .models import Organization
        try:
            obj = Invitation.objects.get(
                email=email, accepted=True
            )
        except Invitation.DoesNotExist:
            o = Organization.objects.create(
                account_type=Organization.FREE
            )
        else:
            o = obj.inviter.organization

        return o

    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        user.organization = self.get_organization(email)
        user.is_superuser = True
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', True)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, 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.')

        return self._create_user(email, password, **extra_fields)


def get_organization(email):
        from invitations.models import Invitation
        from .models import Organization
        try:
            obj = Invitation.objects.get(
                email=email, accepted=True
            )
        except Invitation.DoesNotExist:
            o = Organization.objects.create(
                account_type=Organization.FREE
            )
        else:
            o = obj.inviter.organization

        return o