from django.db import models
from django.conf import settings
from django.urls import reverse
from markdown_it import MarkdownIt
from taggit.managers import TaggableManager

class Post(models.Model):
    title = models.CharField(max_length=200)  # 제목
    contents = models.TextField()  # 본문 (마크다운 저장)
    summary = models.CharField(max_length=2000, blank=True, null=True)  # 요약
    created_at = models.DateTimeField(auto_now_add=True)  # 작성일
    updated_at = models.DateTimeField(auto_now=True)  # 수정일
    tags = TaggableManager()
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        null=True, 
        on_delete=models.CASCADE,
        related_name='blog_posts'
    )

    def render_markdown(self):
        """마크다운을 HTML로 변환"""
        md = MarkdownIt()
        return md.render(self.contents)
    
    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[str(self.pk)])

    def __str__(self):
        return self.title