from django.db import models
import uuid
from shops.models import Shop
from accounts.models import Users


# Create your models here.


class ProductCategory(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True, editable=False)
    category_name = models.CharField(max_length=100)
    category_code = models.CharField(max_length=200, unique=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null= True)
    icon = models.ImageField(upload_to='icons/', null=True, blank=True)
    standard_image = models.ImageField(upload_to='standard_images/', null=True, blank=True)
    banner_image = models.ImageField(upload_to='banners/', null=True, blank=True)
    sales_unit = models.ManyToManyField('shops.Shop', blank= True)
    long_distance_availability = models.BooleanField(default=False)


    def __str__(self):
        return self.category_name


class ProductSubCategory(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True, editable=False)
    sub_category_name = models.CharField(max_length=100)
    sub_category_code = models.CharField(max_length=200, unique=True)
    category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)
    standard_image = models.ImageField(upload_to='standard_images/', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null= True)
    sales_unit = models.ManyToManyField('shops.Shop',  blank=True)
    long_distance_availability = models.BooleanField(default=False)

    def __str__(self):
        return self.sub_category_name


class Products(models.Model):
    product_type = models.CharField(max_length=100, choices=[('Master Product', 'Master Product'), ('Custom Product', 'Custom Product')])
    item_name = models.CharField(max_length=120)
    item_code = models.CharField(max_length=120)
    item_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)
    item_sub_category = models.ForeignKey(ProductSubCategory, on_delete=models.CASCADE)
    item_description = models.TextField(max_length=1300)
    veg_or_non_veg_status = models.CharField(max_length=100, choices=[('Veg', 'Veg'), ('Non Veg', 'Non Veg')])
    i_gst = models.FloatField(max_length=20, null= True, blank=True)
    s_gst = models.FloatField(max_length=20,null= True, blank=True)
    c_gst = models.FloatField(max_length=20,null= True, blank=True)
    cess = models.FloatField(max_length=20, null= True, blank=True)
    created_date = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null=True)
    sales_unit = models.ManyToManyField('shops.Shop', blank=True)
    long_distance_availability = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.item_category} - {self.item_sub_category} - {self.item_name}"
    
class SKU(models.Model):
    product = models.ForeignKey(Products, related_name='skus', on_delete=models.CASCADE)
    sku_name = models.CharField(max_length=120)
    sku_code = models.CharField(max_length=120)
    sku_quantity = models.PositiveIntegerField()
    sku_unit = models.CharField(max_length=20)  
    sku_mrp = models.FloatField(max_length=20,null= True, blank=True)
    sku_expiry_duration = models.PositiveIntegerField()  
    sku_bulk_qty_limit = models.PositiveIntegerField()  
    sku_status = models.CharField(max_length=100, choices=[('Visible', 'Visible'), ('Disabled', 'Disabled'), ('Out of Stock', 'Out of Stock')])
    created_at = models.DateTimeField(auto_now_add=True)
    sales_unit = models.ManyToManyField('shops.Shop', blank = True)
    long_distance_availability = models.BooleanField(default=False)

    def __str__(self):
        return f"SKU for {self.product.item_name}: {self.sku_name}"


class ProductImage(models.Model):
    product = models.ForeignKey(Products, related_name='images', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='product_images/')
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Image for {self.product.item_name}"


class ProductVideo(models.Model):
    product = models.ForeignKey(Products, related_name='videos', on_delete=models.CASCADE)
    video = models.FileField(upload_to='product_videos/')
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Video for {self.product.item_name}"

class ProductReview(models.Model):
    user = models.ForeignKey(Users, on_delete=models.CASCADE)  
    product = models.ForeignKey(Products, related_name='reviews', on_delete=models.CASCADE)  
    rating = models.PositiveIntegerField(choices=[(1, '1 Star'), (2, '2 Stars'), (3, '3 Stars'), (4, '4 Stars'), (5, '5 Stars')])
    review_heading = models.CharField(max_length=150)
    review_text = models.TextField()

    created_at = models.DateTimeField(auto_now_add=True)  # Date of creation

    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name}'s review on {self.product.item_name}"

    class Meta:
        ordering = ['-created_at']

class SalesUnitProductSelection(models.Model):
    sales_unit = models.ForeignKey(Shop, on_delete=models.CASCADE)
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, blank = True , null= True)
    # status = models.CharField(max_length=100, choices=[('enable', 'enable'), ('disable', 'disable')])
    shop_admin_status = models.CharField(max_length=100, choices = [('Visible', 'Visible'), ('Disabled', 'Disabled'),('Out of Stock', 'Out of Stock')], default='Disabled')
    status = models.CharField(max_length=100, choices = [('Visible', 'Visible'), ('Disabled', 'Disabled'),('Out of Stock', 'Out of Stock')])


class Tags(models.Model):
    tag_name = models.CharField(max_length=35)
    icon = models.ImageField(upload_to='icons/', null=True, blank=True)
    products = models.ManyToManyField(Products)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.tag_name


class DynamicFiltering(models.Model):
    filter_name = models.CharField(max_length=35)
    products = models.ManyToManyField(Products)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.filter_name


class SpecialList(models.Model):
    special_name = models.CharField(max_length=35)
    icon = models.ImageField(upload_to='icons/', null=True, blank=True)
    standard_image = models.ImageField(upload_to='standard_images/', null=True, blank=True)
    banner_image = models.ImageField(upload_to='banners/', null=True, blank=True)
    products = models.ManyToManyField(Products)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.special_name


class CustomProduct(models.Model):
    item_name = models.CharField(max_length=120)
    item_code = models.CharField(max_length=120)
    item_description = models.TextField(max_length=1300)
    veg_or_non_veg_status = models.CharField(max_length=100, choices=[('Veg', 'Veg'), ('Non Veg', 'Non Veg')])

    # Size in terms of minimum and maximum size with units
    min_size = models.PositiveIntegerField(help_text="Minimum size of the product")  # Minimum size
    max_size = models.PositiveIntegerField(help_text="Maximum size of the product")  # Maximum size
    size_unit = models.CharField(max_length=20, choices=[('g', 'g'), ('kg', 'kg'), ('ml', 'ml'), ('l', 'l')], default='g', help_text="Unit of size (grams, kilograms, milliliters, etc.)")
    AVAILABILITY_CHOICES = [
        ('Available', 'Available'),
        ('Disabled', 'Disabled'),
        ('Out of Stock', 'Out of Stock'),
    ]
    availability = models.CharField(max_length=20, choices=AVAILABILITY_CHOICES, default='Available')
    created_date = models.DateTimeField(auto_now_add=True, null=True)
    updated_date = models.DateTimeField(auto_now=True, null=True)

    def __str__(self):
        return f"{self.item_name} - {self.veg_or_non_veg_status}"

class CustomProductImage(models.Model):
    custom_product = models.ForeignKey(CustomProduct, related_name='images', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='custom_product_images/')
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Image for {self.custom_product.item_name}"

class CustomProductVideo(models.Model):
    custom_product = models.ForeignKey(CustomProduct, related_name='videos', on_delete=models.CASCADE)
    video = models.FileField(upload_to='custom_product_videos/')
    created_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Video for {self.custom_product.item_name}"

class Wishlist(models.Model):
    user = models.ForeignKey(Users, on_delete=models.CASCADE)
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        unique_together = ('user', 'product')

    def __str__(self):
        return f"Wishlist item for {self.user.first_name} {self.user.last_name}: {self.product.item_name}"