"""ITs for the Brief model & view_post's mark-read behavior.

Brief is a notification record — owner + post FK + line FK + is_unread + kind.
It rides on a Post (one-Post-per-category, Lines accumulate). Clicking FYI on
a Brief banner navigates to billboard:view_post for the underlying Post; that
GET is the contract that flips is_unread → False.
"""

from django.test import TestCase
from django.urls import reverse

from apps.billboard.models import Brief, Line, Post
from apps.lyric.models import User


class BriefModelTest(TestCase):
    def setUp(self):
        self.user = User.objects.create(email="brief@test.io")
        self.post = Post.objects.create(owner=self.user)
        self.line = Line.objects.create(post=self.post, text="Stargazer, 5:21pm", author=self.user)

    def test_brief_defaults_unread(self):
        b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
        self.assertTrue(b.is_unread)

    def test_brief_default_kind_is_user_post(self):
        b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
        self.assertEqual(b.kind, Brief.KIND_USER_POST)

    def test_brief_kind_choices_include_note_unlock_and_share_invite(self):
        choices = dict(Brief._meta.get_field("kind").choices)
        self.assertIn(Brief.KIND_NOTE_UNLOCK, choices)
        self.assertIn(Brief.KIND_USER_POST, choices)
        self.assertIn(Brief.KIND_SHARE_INVITE, choices)

    def test_brief_line_can_be_null(self):
        """A Brief may pre-date its Line (e.g. share-invite spawns the Line
        async — the Brief should still be persistable while the Line write
        is pending). Doesn't break the post FK."""
        b = Brief.objects.create(owner=self.user, post=self.post)
        self.assertIsNone(b.line)

    def test_brief_owner_required(self):
        """Brief without owner is invalid (load-bearing for "whose
        attention"). Post used to be required too, but became nullable
        when GAME_INVITE briefs landed (those use Brief.room instead of
        Brief.post). The view layer enforces "post XOR room" per kind."""
        from django.db import IntegrityError, transaction
        with transaction.atomic(), self.assertRaises(IntegrityError):
            Brief.objects.create(post=self.post, line=self.line)

    def test_brief_carries_title(self):
        b = Brief.objects.create(
            owner=self.user, post=self.post, line=self.line,
            title="Look! — new Note unlocked",
        )
        self.assertEqual(b.title, "Look! — new Note unlocked")

    def test_brief_str_includes_owner_kind_unread(self):
        b = Brief.objects.create(owner=self.user, post=self.post, kind=Brief.KIND_NOTE_UNLOCK)
        s = str(b)
        self.assertIn("brief@test.io", s)
        self.assertIn("note_unlock", s)


class ViewPostMarksReadTest(TestCase):
    """GET /billboard/post/<uuid>/ flips every unread Brief on that post for
    the requesting user to is_unread=False. NVM (banner dismiss client-side
    without nav) leaves Briefs untouched — that path doesn't hit this view."""

    def setUp(self):
        self.user = User.objects.create(email="reader@test.io")
        self.client.force_login(self.user)
        self.post = Post.objects.create(owner=self.user)
        self.line = Line.objects.create(post=self.post, text="entry one", author=self.user)

    def test_get_view_post_flips_owner_unread_brief_to_read(self):
        b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
        self.assertTrue(b.is_unread)
        self.client.get(reverse("billboard:view_post", args=[self.post.id]))
        b.refresh_from_db()
        self.assertFalse(b.is_unread)

    def test_get_does_not_flip_other_users_briefs(self):
        other = User.objects.create(email="other@test.io")
        # Both users have a Brief on this post; only the requesting user's flips
        mine = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
        theirs = Brief.objects.create(owner=other, post=self.post, line=self.line)
        self.client.get(reverse("billboard:view_post", args=[self.post.id]))
        mine.refresh_from_db()
        theirs.refresh_from_db()
        self.assertFalse(mine.is_unread)
        self.assertTrue(theirs.is_unread)

    def test_get_does_not_flip_briefs_on_other_posts(self):
        other_post = Post.objects.create(owner=self.user)
        other_line = Line.objects.create(post=other_post, text="other", author=self.user)
        unrelated = Brief.objects.create(owner=self.user, post=other_post, line=other_line)
        self.client.get(reverse("billboard:view_post", args=[self.post.id]))
        unrelated.refresh_from_db()
        self.assertTrue(unrelated.is_unread)

    def test_get_idempotent_for_already_read_brief(self):
        already_read = Brief.objects.create(
            owner=self.user, post=self.post, line=self.line, is_unread=False,
        )
        self.client.get(reverse("billboard:view_post", args=[self.post.id]))
        already_read.refresh_from_db()
        self.assertFalse(already_read.is_unread)

    def test_post_request_does_not_mark_read(self):
        """Posting a new Line to view_post (the legacy compose flow) is not
        the FYI-read contract — the user is composing, not reviewing. Mark-
        read happens only on a GET render of post.html."""
        b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
        self.client.post(
            reverse("billboard:view_post", args=[self.post.id]),
            data={"text": "appended via POST"},
        )
        b.refresh_from_db()
        self.assertTrue(b.is_unread)
