# Generated by Django 5.2.5 on 2025-09-17 12:50

from django.db import migrations
from itertools import chain


def migrate_old_hero_in_new_streamfield_hero(apps, schema_editor):
    EventEntryPage = apps.get_model("sites_conformes_events", "EventEntryPage")
    EventsIndexPage = apps.get_model("sites_conformes_events", "EventsIndexPage")

    pages = chain(EventEntryPage.objects.all(), EventsIndexPage.objects.all())

    hero_fields = [
        "header_with_title",
        "header_color_class",
        "header_large",
        "header_darken",
        "header_cta_text",
        "header_cta_buttons",
        "header_image",
    ]

    for page in pages:
        old_hero_data = {}

        for field in hero_fields:
            value = getattr(page, field, None)
            if not value:
                continue

            if field == "header_cta_buttons":
                first_item = value[0]
                old_hero_data[field] = first_item.value

            elif field == "header_image":
                old_hero_data[field] = {"image": value, "decorative": True}

            else:
                old_hero_data[field] = value

        if old_hero_data:
            page.hero = [("old_hero", old_hero_data)]
            page.save()


class Migration(migrations.Migration):

    dependencies = [
        ("sites_conformes_events", "0021_evententrypage_hero_eventsindexpage_hero_and_more"),
    ]

    operations = [
        migrations.RunPython(
            migrate_old_hero_in_new_streamfield_hero,
            reverse_code=migrations.RunPython.noop,
            elidable=True,
        )
    ]
