from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, \
                                        PermissionsMixin
from django.conf import settings  # Best practice to import foreign keys


# Create User Manager class
class UserManager(BaseUserManager):

    def create_user(self, email, password=<PASSWORD>, **extra_fields):
        '''Create and saves a new user'''
        if not email:
            raise ValueError('Users must have a valid email address')
        user = self.model(email=self.normalize_email(email), **extra_fields)
        # Note: Use normalize_email helper function to normalise emails
        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_superuser(self, email, password):
        '''Create and save a new superuser'''
        user = self.create_user(email, password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)

        return user


# Create user model
class User(AbstractBaseUser, PermissionsMixin):
    '''Custom user model that supports using email instead of username'''
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    # Creates a new manager
    objects = UserManager()

    # Changes the default username field to email
    USERNAME_FIELD = 'email'

    # Note: Make migrations after creating models


# Create tag model
class Country(models.Model):
    '''Tag to be used for categorising countries'''
    name = models.CharField(max_length=255)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.name


class State(models.Model):
    '''States in a Country'''
    name = models.CharField(max_length=255)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Place(models.Model):
    '''Place object'''
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    name = models.CharField(max_length=255)
    state = models.ManyToManyField('State')
    country = models.ManyToManyField('Country')

    def __str__(self):
        return self.name
