# Generated by Django 5.0.2 on 2024-03-04 14:54

from django.db import migrations
from wagtail.blocks.migrations.migrate_operation import MigrateStreamData
from wagtail.blocks.migrations.operations import RemoveStreamChildrenOperation, RenameStreamChildrenOperation


def update_header_fields(apps, schema_editor):
    """
    Update the header settings for pages that had a hero block
    in the stream
    """

    ContentPage = apps.get_model("sites_conformes_content_manager", "ContentPage")
    for page in ContentPage.objects.all():
        for block in page.body.raw_data:
            if block["type"] == "hero":
                hero_settings = block["value"]
                page.header_with_title = True

                if "bg_image" in hero_settings:
                    page.header_image__id = hero_settings["bg_image"]

                if "bg_color_class" in hero_settings:
                    page.header_color_class = hero_settings["bg_color_class"]

                if "large" in hero_settings:
                    page.header_large = hero_settings["large"]

                if "darken" in hero_settings:
                    page.header_darken = hero_settings["darken"]

                if "cta_link" in hero_settings:
                    page.header_cta_link = hero_settings["cta_link"]

                if "cta_label" in hero_settings:
                    page.header_cta_label = hero_settings["cta_label"]

                page.save()


class Migration(migrations.Migration):
    dependencies = [
        ("sites_conformes_content_manager", "0015_alter_contentpage_options_and_more"),
    ]

    operations = [
        migrations.RunPython(update_header_fields, elidable=True),
        MigrateStreamData(
            app_name="sites_conformes_content_manager",
            model_name="ContentPage",
            field_name="body",
            operations_and_block_paths=[
                (RemoveStreamChildrenOperation(name="hero"), "body"),
                (RemoveStreamChildrenOperation(name="title"), "body"),
                (RenameStreamChildrenOperation(old_name="paragraphlarge", new_name="paragraph"), ""),
            ],
        ),
    ]
