from django.test import TestCase
from django.contrib.auth import get_user_model

from .models import Post, PostCategory


class CmsModelsTests(TestCase):
    def setUp(self):
        self.user = get_user_model().objects.create_user(username="writer", password="x")

    def test_create_category_and_post(self):
        cat = PostCategory.objects.create(name="News")
        self.assertTrue(cat.slug)

        post = Post.objects.create(
            title="Hello World",
            category=cat,
            author=self.user,
            content="Body",
            status=Post.STATUS_DRAFT,
        )
        self.assertTrue(post.slug)
        self.assertEqual(post.status, Post.STATUS_DRAFT)

    def test_publish_sets_published_at(self):
        cat = PostCategory.objects.create(name="Updates")
        post = Post.objects.create(title="Release", category=cat, content="v1", status=Post.STATUS_PUBLISHED)
        self.assertIsNotNone(post.published_at)

    def test_markdown_renders_to_html(self):
        cat = PostCategory.objects.create(name="Docs")
        post = Post.objects.create(
            title="Markdown Test",
            category=cat,
            content="# Hello\n\nThis is **bold**.",
            status=Post.STATUS_DRAFT,
        )
        # Rendered HTML should be populated
        self.assertIsNotNone(post.content_html)
        self.assertIn("<h1>", post.content_html)
        self.assertIn("<strong>", post.content_html)
