from django.db import models
import uuid
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from products.models import Products


# Create your models here.
class CustomInquiryPagination(PageNumberPagination):
    page_size = 15
    page_size_query_param = "page_size"
    max_page_size = 100

    def get_paginated_response(self, data):
        return Response(
            {
                "count": self.page.paginator.count,
                "next": self.get_next_link(),
                "previous": self.get_previous_link(),
                "results": data,
            }
        )


class Inquiries(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    first_name = models.CharField(max_length=255, null=True, blank=True)
    last_name = models.CharField(max_length=255, null=True, blank=True)
    email = models.EmailField()
    contact_number = models.CharField(max_length=500)
    inquiry = models.TextField()
    status = models.CharField(max_length=255, default="pending")
    product = models.ForeignKey(
        Products,
        on_delete=models.CASCADE,
        related_name="product_inquiries",
        null=True,
        blank=True,
    )

    def __str__(self):
        return f"Inquiry by {self.email} submitted on {self.created_at.strftime('%B %d, %Y')} | {self.status}"

    def save(self, *args, **kwargs):
        self.status = self.status.lower()

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