# Generated by Django 5.2.1 on 2025-05-23 15:59

from django.db import migrations
from sites_conformes.blog.models import BlogIndexPage, BlogEntryPage
from itertools import chain


def migrate_obsolete_fields(apps, schema_editor):
    BlogEntryPage = apps.get_model("sites_conformes_blog", "BlogEntryPage")
    BlogIndexPage = apps.get_model("sites_conformes_blog", "BlogIndexPage")
    pages = chain(BlogEntryPage.objects.all(), BlogIndexPage.objects.all())

    for page in pages:
        if not page.header_cta_label and not page.header_cta_link:
            continue

        if page.header_cta_buttons:
            continue

        button_block = (
            "button",
            {
                "text": page.header_cta_label,
                "button_type": "",
                "icon_class": "",
                "icon_side": "",
                "external_url": page.header_cta_link,
            },
        )

        page.header_cta_buttons = [("buttons", [button_block])]
        page.save()


class Migration(migrations.Migration):

    dependencies = [
        ("sites_conformes_blog", "0045_alter_blogentrypage_body_alter_blogindexpage_body_and_more"),
    ]

    operations = [migrations.RunPython(migrate_obsolete_fields, elidable=True)]
