"""Seed default NotificationTemplate rows.

Idempotent: rows that already exist are left alone unless --force is passed.
"""

from __future__ import annotations

from django.core.management.base import BaseCommand

from apps.core.notifications.default_templates import DEFAULT_TEMPLATES
from apps.core.notifications.models import NotificationTemplate


class Command(BaseCommand):
    help = "Seed default NotificationTemplate rows for known verbs."

    def add_arguments(self, parser):
        parser.add_argument(
            "--force",
            action="store_true",
            help="Overwrite existing rows with the seeded defaults.",
        )
        parser.add_argument(
            "--volume",
            choices=("minimal", "demo", "heavy"),
            default="demo",
            help="Accepted for API symmetry with seed_all; ignored here.",
        )

    def handle(self, *args, **options):
        force = options.get("force", False)
        created_count = 0
        updated_count = 0
        skipped_count = 0
        for entry in DEFAULT_TEMPLATES:
            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,
            }
            obj, created = NotificationTemplate.objects.get_or_create(verb=verb, defaults=defaults)
            if created:
                created_count += 1
                continue
            if force:
                for k, v in defaults.items():
                    setattr(obj, k, v)
                obj.save(update_fields=list(defaults.keys()))
                updated_count += 1
            else:
                skipped_count += 1
        self.stdout.write(
            self.style.SUCCESS(
                f"NotificationTemplate seed: created={created_count} updated={updated_count} skipped={skipped_count}"
            )
        )
