from django.db import models
from django.db.models.signals import pre_save

from products.models import Product
from ecommerce.utils import unique_slug_generator


class ProductTag(models.Model):
    """Define the Tag Model"""

    title = models.CharField(max_length=50)
    slug = models.SlugField()
    timestamp = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)
    products = models.ManyToManyField(Product, blank=True)

    def __str__(self):
        return self.title


def tag_pre_save(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = unique_slug_generator(instance)


pre_save.connect(tag_pre_save, ProductTag)
