"""Seed default candidate-notification templates (Email / WhatsApp / SMS) shown
on /master-data/notification-templates.

Run:  python master_seeder/seed_notification_templates.py

IDEMPOTENT — uses update_or_create keyed on (name, channel), so re-running
UPDATES the existing row instead of inserting a duplicate. Existing rows the
seeder doesn't define are left untouched.

NOTE for WhatsApp/SMS: `provider_template_name` must match the template
pre-approved in your gateway account (send2.digital for WhatsApp, DLT content-id
for SMS). It's left blank here — set it on the notification-templates page once
the provider approves the template, or fill PROVIDER_NAMES below.
"""

import django
import os
import sys

# Readable body uses {{placeholders}}; on WhatsApp these map positionally to the
# provider's approved {#var#} slots, in the order they appear.
TEMPLATES = [
    {
        "name": "Whatsapp Job Alert",
        "channel": "WHATSAPP",
        "purpose": "GENERAL",
        "subject": "",
        # Commas placed so the filled sentence has exactly 3 comma-parts, which
        # is what the send2.digital template ({#var#}×3) expects.
        "body": (
            "Hi {{first_name}}, We are hiring for '{{job_title}}', with one of the "
            "leading company. If Interested pls apply {{apply_link}} "
            "-Indovision Services"
        ),
        "provider_template_name": "",  # set the send2.digital approved name here
        "is_active": True,
    },
    {
        "name": "Job Alert",
        "channel": "EMAIL",
        "purpose": "GENERAL",
        "subject": "A {{job_title}} role that matches your profile",
        "body": (
            "Hi {{first_name}},\n\n"
            "We are hiring for {{job_title}} with one of the leading companies. "
            "If interested, please apply here: {{apply_link}}\n\n"
            "Regards,\nIndovision Services"
        ),
        "provider_template_name": "",
        "is_active": True,
    },
]


def seed_notification_templates():
    from apps.notifications.models import NotificationTemplate

    upserted = 0
    for t in TEMPLATES:
        obj, created = NotificationTemplate.objects.update_or_create(
            name=t["name"],
            channel=t["channel"],
            defaults={
                "purpose": t["purpose"],
                "subject": t["subject"],
                "body": t["body"],
                # Do NOT clobber a provider name that was set in the UI: only
                # write it when the seeder actually specifies one.
                **({"provider_template_name": t["provider_template_name"]}
                   if t["provider_template_name"] else {}),
                "is_active": t["is_active"],
            },
        )
        upserted += 1
        print(f"  {'created' if created else 'updated'}: [{obj.channel}] {obj.name}")

    print(f"Notification templates upserted: {upserted} "
          f"({NotificationTemplate.objects.count()} total).")


if __name__ == "__main__":
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
    django.setup()
    seed_notification_templates()
