from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.text import slugify


class UserManager(BaseUserManager):

    def create_superuser(self, email, password, **kwargs):
        user = self.model(email=email, is_staff=True, is_superuser=True, **kwargs)
        user.set_password(password)
        user.save()
        return user


class User(AbstractUser):
    slug = models.SlugField(blank=True)
    email = models.EmailField(null=False, blank=False, unique=True)
    # profile_pic = models.ImageField(upload_to='profile_pics', blank=True) TODO

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.username)
        return super(User, self).save(*args, **kwargs)

    objects = UserManager()
