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


"""
BaseUserManager required whenever overriding base user model. AccountManager
provides for our new user model Account.
"""
class AccountManager(BaseUserManager):
    """
    Create and saves a User (Account) with email username and password
    """
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError("Users must have a valid email")

        if not kwargs.get('username'):
            raise ValueError("Users must have a valid username")

        """
        self.model defaults to model defined in settings.AUTH_USER_MODEL.
        Must tell django in settings.py AUTH_USER_MODEL=Account or it will
        think model is default Django User model
        """
        account = self.model(
            email=self.normalize_email(email),
            username=kwargs.get('username')
        )

        account.set_password(password)
        account.save()

        return account

    """
    Creates and saves a superuser with email, username and password
    """
    def create_superuser(self, email, password, **kwargs):
        account = self.create_user(email, password, **kwargs)

        account.is_admin = True
        account.save()

        return account


class Account(AbstractBaseUser):
    """
    Overriding standard Django User model with our own Account model.

    NOTE that you need to set AUTH_USER_MODEL = authentication.Account in the
    project settings.py file.
    """
    email = models.EmailField(max_length=255, unique=True)
    username = models.CharField(max_length=40, unique=True)

    first_name = models.CharField(max_length=40, blank=True)
    last_name = models.CharField(max_length=40, blank=True)
    tagline = models.CharField(max_length=140, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = AccountManager()

    # name of the field on the User model that is used as the unique identifier
    USERNAME_FIELD = 'email'
    # required fields during creation
    REQUIRED_FIELDS = ['username']

    def __unicode__(self):
        return self.email

    def get_full_name(self):
        return ' '.join([self.first_name, self.last_name])

    def get_short_name(self):
        return self.first_name

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin
