"""WS4.3 / FRREP-LD006 — seed the REP_WIKI_PAGE_UPDATED notification template.

Idempotent. Same pattern as 0024.
"""
from __future__ import annotations

from django.db import migrations


def _seed_wiki(apps, schema_editor):
    NotificationTemplate = apps.get_model("core", "NotificationTemplate")
    from apps.core.notifications.default_templates import DEFAULT_TEMPLATES

    entry = next(
        (e for e in DEFAULT_TEMPLATES if str(e["verb"]) == "rep_wiki_page_updated"),
        None,
    )
    if not entry:
        return
    NotificationTemplate.objects.get_or_create(
        verb=entry["verb"],
        defaults={
            "name": entry["name"],
            "subject": entry["subject"],
            "body_text": entry.get("body_text", ""),
            "body_html": entry.get("body_html", ""),
            "headline_default": entry.get("headline_default", ""),
            "action_label_default": entry.get("action_label_default", "Open in IILMP"),
            "is_active": True,
        },
    )


def _noop_reverse(apps, schema_editor):
    return


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0027_alter_notification_verb_and_more"),
    ]

    operations = [
        migrations.RunPython(_seed_wiki, _noop_reverse),
    ]
