"""Per-verb inbox row formatting (live session card + recording ready)."""
from __future__ import annotations

import pytest
from django.urls import reverse

from apps.core.notifications.models import Notification
from apps.core.notifications.services import send_notification


@pytest.fixture
def learner(django_user_model):
    return django_user_model.objects.create_user(email="inbox-l@example.com", password="pw")


@pytest.mark.django_db
def test_data_persists_on_send_notification(learner):
    n = send_notification(
        learner,
        "msg",
        verb=Notification.Verb.REP_LIVE_SESSION_STARTING,
        action_url="/courses/x/live/1/join/",
        data={"minutes_until": 5, "session_title": "Class A", "course_title": "Crops 101"},
    )
    n.refresh_from_db()
    assert n.data == {"minutes_until": 5, "session_title": "Class A", "course_title": "Crops 101"}


@pytest.mark.django_db
def test_data_defaults_to_empty_dict(learner):
    n = send_notification(learner, "plain", verb=Notification.Verb.GENERIC)
    n.refresh_from_db()
    assert n.data == {}


@pytest.mark.django_db
def test_inbox_renders_starting_card_with_join_button(client, learner):
    send_notification(
        learner,
        "Live session starting",
        verb=Notification.Verb.REP_LIVE_SESSION_STARTING,
        action_url="/rep/courses/test/live/1/join/",
        data={
            "course_title": "Sustainable Crops",
            "session_title": "Maize webinar",
            "minutes_until": 3,
            "session_id": 1,
            "course_slug": "test",
            "starts_at": "2026-05-02T15:00:00+00:00",
        },
    )
    client.force_login(learner)
    resp = client.get(reverse("core:notification_inbox"))
    assert resp.status_code == 200
    body = resp.content.decode()
    assert "Maize webinar" in body
    assert "Sustainable Crops" in body
    assert "Starts in 3 minutes" in body
    # Prominent button label, not just chevron — rendered as an <a class="btn-primary">.
    assert "btn-primary" in body
    assert "Join" in body
    assert "/go/" in body  # routed through notification_go (URL: /notifications/<pk>/go/)


@pytest.mark.django_db
def test_inbox_renders_live_now_when_minutes_zero(client, learner):
    send_notification(
        learner,
        "Live now",
        verb=Notification.Verb.REP_LIVE_SESSION_STARTING,
        action_url="/rep/courses/test/live/1/join/",
        data={"course_title": "C", "session_title": "S", "minutes_until": 0},
    )
    client.force_login(learner)
    resp = client.get(reverse("core:notification_inbox"))
    body = resp.content.decode()
    assert "Live now" in body
    assert "Join now" in body


@pytest.mark.django_db
def test_inbox_renders_recording_ready_card(client, learner):
    send_notification(
        learner,
        "Recording available",
        verb=Notification.Verb.REP_LIVE_RECORDING_READY,
        action_url="/rep/courses/test/live/recording/4/play/",
        data={
            "course_title": "Crops",
            "session_title": "Soil intro",
            "recording_id": 4,
            "session_id": 1,
            "course_slug": "test",
        },
    )
    client.force_login(learner)
    resp = client.get(reverse("core:notification_inbox"))
    body = resp.content.decode()
    assert "Soil intro" in body
    assert "Watch recording" in body


@pytest.mark.django_db
def test_inbox_falls_back_to_generic_when_verb_has_no_partial(client, learner):
    """Verbs without a _verb_*.html partial render the generic row."""
    send_notification(
        learner,
        "Generic message goes here",
        verb=Notification.Verb.GENERIC,
        action_url="/anywhere/",
    )
    client.force_login(learner)
    resp = client.get(reverse("core:notification_inbox"))
    body = resp.content.decode()
    assert "Generic message goes here" in body
