from django.db import models
from django.urls import reverse
from django_countries.fields import CountryField
from accounts.models import User
from star_ratings.models import Rating, UserRating
from django.core.validators import MaxValueValidator, MinValueValidator
from django.contrib.contenttypes.fields import GenericRelation
from django.template.defaultfilters import slugify

class Category(models.Model):
    name = models.CharField(max_length=150, db_index=True)
    slug = models.SlugField(max_length=150, unique=True ,db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ('name', )
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('product_list_by_category', args=[self.slug])


class Message(models.Model):
    sender = models.ForeignKey(User, related_name='message_sender', on_delete=models.CASCADE)
    receiver = models.ForeignKey(User, related_name='message_receiver', on_delete=models.CASCADE)
    content = models.TextField(default='')
    create = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)
    check = models.BooleanField(default=False)
    sender_removed = models.BooleanField(default=False)
    receiver_removed = models.BooleanField(default=False)

    def get_sender(self):
        return self.sender

    def get_receiver(self):
        return self.receiver


class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
    name = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, validators=[MinValueValidator(1.0)])
    available = models.BooleanField(default=True)
    stock = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    country = CountryField(null=True)
    productOwnerID = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Owner ID', editable=False, default=1)
    payout_address = models.CharField(max_length=100, default='')
    ratings = GenericRelation(Rating, related_query_name='products')

    class Meta:
        ordering = ('name', )
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
            return reverse('product_detail', args=[self.id, self.slug])


class ShippingOptions(models.Model):
    """docstring for ShippingOptions"""
    zone_name = models.CharField(max_length=100)
    zone_type = models.CharField(max_length=255)
    
    class Meta:
        ordering = ('id',)
    def __str__(self):
        return self.zone_name
        