from django.db import models
from django.utils import timezone


class Category(models.Model):
    name = models.CharField(max_length=128)
    order = models.PositiveIntegerField(default=5)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['order', 'name']
        verbose_name = 'FAQ Category'
        verbose_name_plural = 'FAQ Categories'


class FAQ(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    category = models.ForeignKey(
        Category, related_name='faqs', blank=True,
        null=True, on_delete=models.SET_NULL)
    question = models.CharField(max_length=128)
    answer = models.TextField()
    order = models.PositiveIntegerField(default=5)

    def save(self, *args, **kwargs):
        if not self.id:
            self.created_at = timezone.now()
        self.updated_at = timezone.now()
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.question

    class Meta:
        ordering = ['category', 'order']
        verbose_name = 'FAQ'
        verbose_name_plural = 'FAQs'
