from django.db import models

# Create your models here.
from datetime import date
from django.db import models
from django.contrib.auth.models import PermissionsMixin, AbstractUser, BaseUserManager
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _


class CustomAccountManager(BaseUserManager):
    def create_user(self, username, first_name, last_name, email, password, **other_fields):
        if not email:
            raise ValueError(_('You Must Provide Email'))
        user = self.model(username=username, first_name=first_name, last_name=last_name,
                          email=email, ** other_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, first_name, last_name, username, email, password, **other_fields):
        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)
        if other_fields.get('is_staff') is not True:
            raise ValueError('Super user must be assigned to is_staff=True')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Super user must be assigned to is_superuser=True')
        return self.create_user(username, first_name, last_name, email, password, **other_fields)


class User(AbstractBaseUser, PermissionsMixin):
    first_name = models.CharField(max_length=150, blank=False)
    last_name = models.CharField(max_length=150, blank=True)
    email = models.EmailField(_('email address'), unique=True)
    username = models.CharField(max_length=150, unique=True)
    post_code = models.CharField(max_length=15, blank=True)
    town_city = models.CharField(max_length=150, blank=True)
    phone_number = models.CharField(max_length=10, blank=True, unique=True)
    address_line_1 = models.CharField(max_length=150, blank=True)
    address_line_2 = models.CharField(max_length=150, blank=True)
    is_active = models.BooleanField(default=True)
    password = models.CharField(max_length=150, blank=False, default="")
    created = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(default="default.png",
                              upload_to='images/profile_pics')
    updated = models.DateTimeField(auto_now=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['first_name', 'last_name',
                       'email', 'password']

    objects = CustomAccountManager()

    class Meta:
        ordering = ('username',)
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_short_name(self):
        """
        Returns the display name.
        If full name is present then return full name as display name
        else return username.
        """
        if self.username != '':
            return self.username
        else:
            return self.username
