"""WS5.13 — staff notification template editor."""
from __future__ import annotations

import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse

from apps.core.notifications.models import Notification, NotificationTemplate

User = get_user_model()


@pytest.fixture
def staff(django_user_model):
    return django_user_model.objects.create_user(email="ntpl@x.com", password="x", is_staff=True)


@pytest.mark.django_db
def test_list_blocks_anonymous(client):
    resp = client.get(reverse("core:notification_template_list"))
    assert resp.status_code in (302, 403)


@pytest.mark.django_db
def test_list_blocks_non_staff(client, django_user_model):
    learner = django_user_model.objects.create_user(email="learner-tpl@x.com", password="x")
    client.force_login(learner)
    resp = client.get(reverse("core:notification_template_list"))
    assert resp.status_code == 403


@pytest.mark.django_db
def test_list_renders_for_staff(client, staff):
    client.force_login(staff)
    resp = client.get(reverse("core:notification_template_list"))
    assert resp.status_code == 200
    # Should mention at least one of our REP verbs.
    assert b"rep_certificate_issued" in resp.content


@pytest.mark.django_db
def test_list_filter_by_prefix(client, staff):
    client.force_login(staff)
    resp = client.get(reverse("core:notification_template_list") + "?prefix=rep_")
    body = resp.content.decode()
    assert "rep_certificate_issued" in body
    # Filtered out non-REP
    assert "smehub_business_verified" not in body


@pytest.mark.django_db
def test_edit_starts_from_seed_default(client, staff):
    """Opening the editor for a verb that has no custom row should still
    render the seeded subject as the initial form value."""
    client.force_login(staff)
    NotificationTemplate.objects.filter(verb=Notification.Verb.REP_CERTIFICATE_ISSUED).delete()
    resp = client.get(reverse(
        "core:notification_template_edit",
        kwargs={"verb": Notification.Verb.REP_CERTIFICATE_ISSUED},
    ))
    assert resp.status_code == 200
    body = resp.content.decode()
    # The seed default subject mentions "Certificate issued"
    assert "Certificate issued" in body


@pytest.mark.django_db
def test_save_persists_custom_template(client, staff):
    client.force_login(staff)
    NotificationTemplate.objects.filter(verb=Notification.Verb.REP_CERTIFICATE_ISSUED).delete()
    resp = client.post(
        reverse("core:notification_template_edit",
                kwargs={"verb": Notification.Verb.REP_CERTIFICATE_ISSUED}),
        {
            "name": "Custom cert email",
            "subject": "🎓 Your IILMP certificate is ready",
            "body_text": "Hi {{ recipient_name }} — your certificate is attached.",
            "body_html": "<p>Hi {{ recipient_name }} — your certificate is attached.</p>",
            "headline_default": "Your certificate has arrived",
            "action_label_default": "Open My Certificates",
            "is_active": "on",
        },
    )
    assert resp.status_code == 302
    row = NotificationTemplate.objects.get(verb=Notification.Verb.REP_CERTIFICATE_ISSUED)
    assert "Your IILMP certificate is ready" in row.subject
    assert row.is_active is True


@pytest.mark.django_db
def test_invalid_verb_404(client, staff):
    client.force_login(staff)
    resp = client.get(reverse("core:notification_template_edit", kwargs={"verb": "made_up_verb"}))
    assert resp.status_code in (403, 404)
