from django.db import migrations

TEMPLATES = [
    {
        "subject": "Tai AfroPress Editorial Update",
        "brand": "afropress",
        "preheader": "News from across Tai AfroPress this month.",
        "blocks": [
            ("hero_image", {"image_url": "", "alt_text": "", "headline": "This Month at Tai AfroPress", "subtext": "", "link_url": ""}),
            ("heading", {"text": "From the Publishing Desk", "align": "left", "color": "text"}),
            ("rich_text", {"html": "<p>Share your editorial update here — industry news, a word from the team, or what's coming next.</p>"}),
            ("image_text", {"image_url": "", "alt_text": "", "heading": "Author Feature", "html": "<p>Introduce an author or a story behind a title.</p>", "link_url": "", "image_position": "left"}),
            ("divider", {"color": "border"}),
            ("button", {"label": "Read More", "url": "", "color": "gold", "align": "center"}),
            ("footer_links", {}),
        ],
    },
    {
        "subject": "Tai BookStore Featured Books",
        "brand": "bookstore",
        "preheader": "New releases and reader favorites, picked for you.",
        "blocks": [
            ("hero_image", {"image_url": "", "alt_text": "", "headline": "Featured Books This Week", "subtext": "", "link_url": ""}),
            ("heading", {"text": "New Releases", "align": "left", "color": "rust"}),
            ("book_highlight", {"book_id": None, "badge": "New Release", "button_label": "Shop Now", "description": "", "title_override": "", "price_override": "", "url_override": ""}),
            ("book_highlight", {"book_id": None, "badge": "Featured", "button_label": "Shop Now", "description": "", "title_override": "", "price_override": "", "url_override": ""}),
            ("divider", {"color": "border"}),
            ("button", {"label": "Browse All Books", "url": "", "color": "gold", "align": "center"}),
            ("footer_links", {}),
        ],
    },
    {
        "subject": "African Publishing & Events",
        "brand": "afropress",
        "preheader": "Industry news and upcoming events.",
        "blocks": [
            ("heading", {"text": "African Publishing & Events", "align": "center", "color": "green"}),
            ("rich_text", {"html": "<p>A short introduction to this edition.</p>"}),
            ("image_text", {"image_url": "", "alt_text": "", "heading": "Upcoming Event", "html": "<p>Date, venue, and details.</p>", "link_url": "", "image_position": "right"}),
            ("button", {"label": "RSVP", "url": "", "color": "green", "align": "center"}),
            ("footer_links", {}),
        ],
    },
]


def seed_templates(apps, schema_editor):
    Newsletter = apps.get_model("catalog_app", "Newsletter")
    NewsletterBlock = apps.get_model("catalog_app", "NewsletterBlock")

    for tpl in TEMPLATES:
        if Newsletter.objects.filter(subject=tpl["subject"], is_template=True).exists():
            continue
        newsletter = Newsletter.objects.create(
            subject=tpl["subject"], brand=tpl["brand"], preheader=tpl["preheader"],
            is_template=True, status="draft",
        )
        for position, (block_type, config) in enumerate(tpl["blocks"], start=1):
            NewsletterBlock.objects.create(
                newsletter=newsletter, block_type=block_type, position=position, config=config,
            )


def remove_templates(apps, schema_editor):
    Newsletter = apps.get_model("catalog_app", "Newsletter")
    Newsletter.objects.filter(is_template=True, subject__in=[t["subject"] for t in TEMPLATES]).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('catalog_app', '0048_backfill_newsletter_status'),
    ]

    operations = [
        migrations.RunPython(seed_templates, remove_templates),
    ]
