from datetime import datetime
from shop.models import Product
from django.db import models
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress

# Create your models here.
class Item(Product):
    category = models.ForeignKey('CatalogCategory', related_name='items')
    description = models.TextField()
    photo = models.ImageField(upload_to='item_photo', blank=True)
    manufacturer = models.CharField(max_length=300, blank=True)

    def __unicode__(self):
        return u'%s' % self.name

    @models.permalink
    def get_absolute_url(self):
        return ('shop:product_detail', (), {'slug': self.slug})

    class Meta:
        pass

class Catalog(models.Model):
    name = models.CharField(max_length=300)
    slug = models.SlugField(max_length=150, unique=True)
    publisher = models.CharField(max_length=300)
    description = models.TextField(blank=True)
    pub_date = models.DateTimeField(default=datetime.now)

    def __unicode__(self):
        return u'%s' % self.name

class CatalogCategory(models.Model):
    catalog = models.ForeignKey('Catalog', related_name='categories')
    parent = models.ForeignKey('self', blank=True, null=True,
                               related_name='children')
    name = models.CharField(max_length=300)
    slug = models.SlugField(max_length=150)
    description = models.TextField(blank=True)

    def __unicode__(self):
        if self.parent:
            return u'%s: %s - %s' % (self.catalog.name,
                                     self.parent.name,
                                     self.name)
        return u'%s: %s' % (self.catalog.name, self.name)
		
    class Meta:
        ordering = ["catalog"]
        verbose_name = "Catalog Category"
        verbose_name_plural = "Catalog Categories"

class ItemDetail(models.Model):
    item = models.ForeignKey('Item', related_name='details')
    attribute = models.ForeignKey('ItemAttribute')
    value = models.CharField(max_length=500)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return u'%s: %s' % (self.item, self.attribute)

class ItemAttribute(models.Model):
    name = models.CharField(max_length=300)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return u'%s' % self.name
 
class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')
 
    def __unicode__(self):
        return "{}'s profile".format(self.user.username)
 
    class Meta:
        db_table = 'user_profile'
 
    def account_verified(self):
        if self.user.is_authenticated:
            result = EmailAddress.objects.filter(email=self.user.email)
            if len(result):
                return result[0].verified
        return False
 
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
