from django.conf import settings
from django.db import models

# Using string reference for CloudflareImage
from django_cloudflareimages_toolkit.models import CloudflareImage

from apps.core.models import SluggedModel


class Tag(SluggedModel):
    name = models.CharField(max_length=50, unique=True)

    class Meta:
        ordering = ["name"]

    def __str__(self):
        return self.name


class Post(SluggedModel):
    title = models.CharField(max_length=255)
    content = models.TextField(help_text="Markdown content supported")
    excerpt = models.TextField(blank=True, help_text="Short summary for lists")

    image = models.ForeignKey(
        CloudflareImage,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="blog_posts",
        help_text="Featured image",
    )

    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="blog_posts")

    published_at = models.DateTimeField(null=True, blank=True, db_index=True)
    is_published = models.BooleanField(default=False, db_index=True)

    tags = models.ManyToManyField(Tag, blank=True, related_name="posts")

    class Meta:
        ordering = ["-published_at", "-created_at"]

    def __str__(self):
        return self.title
