# Generated by Django 5.2.7 on 2026-05-03

import django.db.models.deletion
from django.db import migrations, models
import wagtail.fields


def copy_newsletter_template_settings(apps, schema_editor):
    SystemNotificationMailSettings = apps.get_model("base", "SystemNotificationMailSettings")
    NewsletterTemplateSettings = apps.get_model("base", "NewsletterTemplateSettings")

    src = NewsletterTemplateSettings.objects.order_by("id").first()
    if not src:
        return
    for target in SystemNotificationMailSettings.objects.all():
        target.subscribe_subject_template = src.subscribe_subject_template
        target.subscribe_html_template = src.subscribe_html_template
        target.subscribe_text_template = src.subscribe_text_template
        target.confirm_success_template = src.confirm_success_template
        target.confirm_failure_template = src.confirm_failure_template
        target.unsubscribe_intro_template = src.unsubscribe_intro_template
        target.unsubscribe_success_template = src.unsubscribe_success_template
        target.unsubscribe_failure_template = src.unsubscribe_failure_template
        target.save(
            update_fields=[
                "subscribe_subject_template",
                "subscribe_html_template",
                "subscribe_text_template",
                "confirm_success_template",
                "confirm_failure_template",
                "unsubscribe_intro_template",
                "unsubscribe_success_template",
                "unsubscribe_failure_template",
            ]
        )


class Migration(migrations.Migration):
    dependencies = [
        ("base", "0008_alter_contactformsubmission_options_and_more"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="newslettercampaign",
            options={
                "ordering": ["-created_at"],
                "permissions": [("send_newslettercampaign", "Can send newsletter campaign")],
                "verbose_name": "Newsletter Campaign",
                "verbose_name_plural": "Newsletter Campaigns",
            },
        ),
        migrations.CreateModel(
            name="NewsletterTemplate",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=255, verbose_name="Name")),
                ("subject", models.CharField(blank=True, default="", max_length=255, verbose_name="Subject")),
                ("template_json", models.JSONField(blank=True, default=dict, verbose_name="Template JSON")),
                ("template_html", models.TextField(blank=True, default="", verbose_name="Template HTML")),
                ("template_text", models.TextField(blank=True, default="", verbose_name="Template Text")),
                ("created_at", models.DateTimeField(auto_now_add=True, verbose_name="Created At")),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="Updated At")),
            ],
            options={
                "verbose_name": "Newsletter Template",
                "verbose_name_plural": "Newsletter Templates",
                "ordering": ["-updated_at", "-created_at"],
            },
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="confirm_failure_template",
            field=wagtail.fields.RichTextField(blank=True, default="<p>訂閱確認失敗，請稍後再試。</p>", verbose_name="Confirm Failure Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="confirm_success_template",
            field=wagtail.fields.RichTextField(blank=True, default="<p>訂閱確認成功。</p>", verbose_name="Confirm Success Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="subscribe_html_template",
            field=models.TextField(default="<p>您好，請點擊以下連結完成訂閱：</p><p><a href='{{confirm_url}}'>{{confirm_url}}</a></p>", verbose_name="Subscribe HTML Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="subscribe_subject_template",
            field=models.CharField(default="請確認您的電子報訂閱", max_length=255, verbose_name="Subscribe Subject Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="subscribe_text_template",
            field=models.TextField(default="您好，請點擊以下連結完成訂閱：{{confirm_url}}", verbose_name="Subscribe Text Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="unsubscribe_failure_template",
            field=wagtail.fields.RichTextField(blank=True, default="<p>退訂失敗，請稍後再試。</p>", verbose_name="Unsubscribe Failure Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="unsubscribe_intro_template",
            field=wagtail.fields.RichTextField(blank=True, default="<p>確認要退訂電子報嗎？</p>", verbose_name="Unsubscribe Intro Template"),
        ),
        migrations.AddField(
            model_name="systemnotificationmailsettings",
            name="unsubscribe_success_template",
            field=wagtail.fields.RichTextField(blank=True, default="<p>已完成退訂。</p>", verbose_name="Unsubscribe Success Template"),
        ),
        migrations.RunPython(copy_newsletter_template_settings, migrations.RunPython.noop),
        migrations.AddField(
            model_name="newslettercampaign",
            name="newsletter_template",
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="campaigns", to="base.newslettertemplate", verbose_name="Newsletter Template"),
        ),
        migrations.AlterField(
            model_name="newslettercampaign",
            name="html_template",
            field=wagtail.fields.RichTextField(verbose_name="HTML Template"),
        ),
    ]
