from django.db import models
import random
import os
from django.db import models
from django.db.models.signals import pre_save
from ecommerce.utils import unique_slug_generator
from django.urls import reverse
from django.db.models import Q


def get_filename_ext(filepath):
    base_name = os.path.basename(filepath)
    name,ext = os.path.splitext(base_name)
    print("base_name",base_name)
    print("name",name)
    print("ext",ext)
    return name,ext

def upload_image_path(instance,filename):
    print("instance",instance)
    print("filename",filename)
    new_filename = random.randint(1,2222222222)
    name,ext = get_filename_ext(filename)
    final_filename = '{new_filename}{ext}'.format(new_filename=new_filename,ext=ext)
    return "products/{new_filename}/{final_filename}".format(
            new_filename = new_filename,
            final_filename = final_filename
        )

class ProductQuerySet(models.query.QuerySet):
    def active(self):
        return self.filter(active=True)
    
    def featured(self):
        return self.filter(featured=True)

    def search(self,query):
        lookups = (Q(title__icontains=query)|
                  Q(description__icontains=query)|
                  Q(price__icontains=query)|
                  Q(tag__title__icontains=query)|
                  Q(tag__slug=query))

        return self.filter(lookups).active().distinct()

class ProductManager(models.Manager):
    """
        Model is register for custome model manager
    """
    def get_queryset(self):
        return ProductQuerySet(self.model,using=self._db)

    def featured(self):
        return self.get_queryset().featured()

    """
        Using model manager
    """
    # def  featured(self):
    #     return self.get_queryset().filter(featured=True)
    
    def get_by_id(self,id):
        qs = self.get_queryset().filter(id=id)
        if qs.count() == 1:
            return qs.first()
        return None
    
    def search(self,query):
        return self.get_queryset().search(query)

# Create your models here.
class Product(models.Model):
    title = models.CharField(max_length=150)
    slug = models.SlugField(blank=True,unique=True)
    description = models.TextField()
    price = models.DecimalField(decimal_places=2,max_digits=20,null=True)
    image = models.FileField(upload_to=upload_image_path , null=True,blank=True)
    featured = models.BooleanField(default=False)
    active  = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    objects = ProductManager()

    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        #return "/products/{slug}/" . format(slug=self.slug)
        return reverse("products:detail" , kwargs={"slug":self.slug})

    

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

pre_save.connect(product_pre_save_receiver,sender=Product)