from django.db import models
import uuid
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from django.contrib.postgres.fields import ArrayField
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django_quill.fields import QuillField


class CustomTwentyPagesPagination(PageNumberPagination):
    page_size = 20
    page_size_query_param = "page_size"
    max_page_size = 100

    def get_paginated_response(self, data):
        next_page_link = self.get_next_link()
        previous_page_link = self.get_previous_link()

        return Response(
            {
                "count": self.page.paginator.count,
                "next": next_page_link if next_page_link else "",
                "previous": previous_page_link if previous_page_link else "",
                "results": data,
            }
        )


# Create your models here.
class Catalogs(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=500)
    image_path = models.TextField()
    link = models.TextField()

    def __str__(self):
        return self.title


class Categories(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    main_category_name = ArrayField(models.CharField(max_length=500))
    sub_category_name = models.CharField(max_length=255)
    link = models.TextField(null=True, blank=True)
    is_active = models.BooleanField(default=True)
    multiple_orderings = models.JSONField(null=True, blank=True)
    meta_description = models.TextField(null=True, blank=True)
    meta_keywords = models.CharField(null=True, blank=True)

    def __str__(self):
        return f"{self.main_category_name} - {self.sub_category_name} | {'Active' if self.is_active else 'Inactive'}"

    def save(self, *args, **kwargs):
        titled_main_category = []

        for category in self.main_category_name:
            category = category.title()
            titled_main_category.append(category)

        self.main_category_name = titled_main_category
        self.sub_category_name = self.sub_category_name.title()

        super(Categories, self).save(*args, **kwargs)


class Series(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return f"{self.name} | {'Active' if self.is_active else 'Inactive'}"

    def save(self, *args, **kwargs):
        self.name = self.name.title()
        super(Series, self).save(*args, **kwargs)


class Products(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255)
    image_path = models.TextField(null=True, blank=True)
    edp_number = models.CharField(max_length=255)
    article_number = models.CharField(max_length=255, null=True, blank=True)
    description = QuillField(null=True, blank=True)
    finish = models.CharField(max_length=255, null=True, blank=True)
    size = models.CharField(max_length=255, null=True, blank=True)
    category = models.ForeignKey(
        Categories, on_delete=models.CASCADE, related_name="category_products"
    )
    price = models.FloatField(default=0.00)
    is_active = models.BooleanField(default=True)
    sold_as_set = models.BooleanField(default=False)
    is_discounted = models.BooleanField(default=False)
    discount_rate = models.PositiveIntegerField(default=0)
    discount_valid_until = models.DateTimeField(null=True, blank=True)
    quantity = models.PositiveBigIntegerField(default=0)
    series = models.ForeignKey(
        Series,
        on_delete=models.SET_NULL,
        related_name="series_products",
        null=True,
        blank=True,
    )
    arrival_date = models.DateTimeField(null=True, blank=True)
    is_new_arrival = models.BooleanField(default=False)
    is_coming_soon = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.name} - {self.edp_number} | {'Active' if self.is_active else 'Inactive'}"

class NewArrivalProducts(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    product = models.ForeignKey(
        Products, on_delete=models.CASCADE, related_name="new_arrival_products"
    )

    def __str__(self):
        return f"{self.product.name} - {self.product.edp_number}"

class FeaturedProducts(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)

    # the required fields to enable a generic relation
    model = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    model_object_id = models.UUIDField()
    model_object = GenericForeignKey("model", "model_object_id")

    product = models.ForeignKey(
        Products, on_delete=models.CASCADE, related_name="featured_products"
    )

    def __str__(self):
        return f"{self.product.name} in {self.model} {self.model_object_id}"


class ProductGallery(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    image_path = models.TextField()
    product = models.ForeignKey(
        Products, on_delete=models.CASCADE, related_name="product_images"
    )
    is_shown = models.BooleanField(default=True)

    def __str__(self):
        return f"Image of {self.product.name}"


class NewsAndPressReleases(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=255)
    short_description = QuillField(null=True, blank=True)
    long_description = QuillField(null=True, blank=True)
    image_path = models.TextField(null=True, blank=True)
    link = models.TextField(null=True, blank=True)
    type = models.CharField(max_length=255)  # News or Press Release

    def __str__(self):
        return f"{self.title} - {self.type.upper()}"


class NewsGallery(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    news = models.ForeignKey(
        NewsAndPressReleases, on_delete=models.CASCADE, related_name="news_images"
    )
    image_path = models.TextField()

    def __str__(self):
        return f"Image of '{self.news.title}'"


class Settings(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255)
    type = models.CharField(max_length=255, default="toggle")
    options = ArrayField(models.CharField(max_length=500), null=True, blank=True)
    value = models.CharField(max_length=500, null=True, blank=True)

    def __str__(self):
        return self.name
