from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, \
    PermissionsMixin
from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator


class UserManager(BaseUserManager):
    def create_user(self, email, username, password, **extra_fields):
        """"Create and save a new user"""
        if not email:
            raise ValueError('Users must have an email address')
        if not username:
            raise ValueError('Users must have a username')
        user = self.model(email=self.normalize_email(email),
                          **extra_fields)
        user.username = username
        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_superuser(self, email, username, password):
        """Creattes and saves a new superuser"""
        user = self.create_user(email, username, password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)

        return user


class User(AbstractBaseUser, PermissionsMixin):
    """"Custom user model that uses email and unique username"""
    email = models.EmailField(max_length=255, unique=True)
    username = models.CharField(max_length=255, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=True)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    class Meta:
        unique_together = ('email', 'username',)


class Tag(models.Model):
    """Tag to be given to a Tv Show"""
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    name = models.CharField(max_length=255)

    def __str__(self):
        return str(self.name)


class Rating(models.Model):
    """Rating to be given to a tvshow"""
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    rtng = models.IntegerField(validators=[MinValueValidator(0),
                               MaxValueValidator(10)])

    def __int__(self):
        return int(self.rtng)


class TvShow(models.Model):
    """TvShow object"""
    title = models.CharField(max_length=255)
    sypnosis = models.TextField()
    airdate = models.DateField(auto_now_add=False)
    genre = models.CharField(max_length=50)
    tags = models.ManyToManyField('Tag')
    ratings = models.ManyToManyField('Rating')

    def __str__(self):
        return self.title
