# Generated by Django 5.2.3 on 2025-09-29 14:06

import django.db.models.deletion
from django.db import migrations, models, connection


def drop_page_ptr(apps, schema_editor):
    """
    We have to do some stupid workaround for deleting the page_ptr_id for sqlite.

    Somehow the normal migration does not do what we want and page_ptr_id is not removed from the table for sqlite.

    Hence we do the following manually, which does work!

    1. Creating a new temporary table without the unwanted column.
    2. Copying all existing data over.
    3. Dropping the old table.
    4. Renaming the new table.


    For postgres we do the normal operation, but we do it in the RunPython method because it's conditional.
    """

    if connection.vendor == "sqlite":
        with connection.cursor() as cursor:
            cursor.execute("PRAGMA foreign_keys=off;")

            cursor.execute("""
                CREATE TABLE new_coyote_coyotehomepage (
                    abstractbasepage_ptr_id integer NOT NULL PRIMARY KEY 
                        REFERENCES "ocyan_plugin_wagtail_abstractbasepage" ("page_ptr_id") DEFERRABLE INITIALLY DEFERRED,
                    content text NOT NULL CHECK ((JSON_VALID("content") OR "content" IS NULL)),
                    footer text NOT NULL CHECK ((JSON_VALID("footer") OR "footer" IS NULL)),
                    header text NOT NULL CHECK ((JSON_VALID("header") OR "header" IS NULL))
                );
            """)
            cursor.execute("""
                INSERT INTO new_coyote_coyotehomepage (abstractbasepage_ptr_id, content, footer, header)
                SELECT abstractbasepage_ptr, content, footer, header FROM coyote_coyotehomepage;
            """)
            cursor.execute("DROP TABLE coyote_coyotehomepage;")
            cursor.execute(
                "ALTER TABLE new_coyote_coyotehomepage RENAME TO coyote_coyotehomepage;"
            )

            cursor.execute("""
                CREATE TABLE new_coyote_coyotecontactpage (
                    abstractbasepage_ptr_id integer NOT NULL PRIMARY KEY 
                        REFERENCES "ocyan_plugin_wagtail_abstractbasepage" ("page_ptr_id") DEFERRABLE INITIALLY DEFERRED,
                    header text NOT NULL CHECK ((JSON_VALID("header") OR "header" IS NULL)),
                    content text NOT NULL CHECK ((JSON_VALID("content") OR "content" IS NULL)),
                    footer text NOT NULL CHECK ((JSON_VALID("footer") OR "footer" IS NULL))
                );
            """)
            cursor.execute("""
                INSERT INTO new_coyote_coyotecontactpage (abstractbasepage_ptr_id, header, content, footer)
                SELECT abstractbasepage_ptr, header, content, footer FROM coyote_coyotecontactpage;
            """)
            cursor.execute("DROP TABLE coyote_coyotecontactpage;")
            cursor.execute(
                "ALTER TABLE new_coyote_coyotecontactpage RENAME TO coyote_coyotecontactpage;"
            )

            cursor.execute("PRAGMA foreign_keys=on;")
    else:
        CoyoteContactPage = apps.get_model("coyote", "CoyoteContactPage")
        CoyoteHomePage = apps.get_model("coyote", "CoyoteHomePage")

        schema_editor.remove_field(
            CoyoteContactPage, CoyoteContactPage._meta.get_field("page_ptr")
        )
        schema_editor.remove_field(
            CoyoteContactPage, CoyoteContactPage._meta.get_field("search_image")
        )
        schema_editor.remove_field(
            CoyoteHomePage, CoyoteHomePage._meta.get_field("page_ptr")
        )
        schema_editor.remove_field(
            CoyoteHomePage, CoyoteHomePage._meta.get_field("search_image")
        )

        new_homepagefield = models.OneToOneField(
            "ocyan_plugin_wagtail.AbstractBasePage",
            on_delete=django.db.models.deletion.CASCADE,
            auto_created=True,
            parent_link=True,
            primary_key=True,
            serialize=False,
        )

        new_homepagefield.set_attributes_from_name("abstractbasepage_ptr")
        new_homepagefield.model = CoyoteContactPage
        new_homepagefield.contribute_to_class(CoyoteContactPage, "abstractbasepage_ptr")

        new_contactfield = models.OneToOneField(
            "ocyan_plugin_wagtail.AbstractBasePage",
            on_delete=django.db.models.deletion.CASCADE,
            auto_created=True,
            parent_link=True,
            primary_key=True,
            serialize=False,
        )

        new_contactfield.set_attributes_from_name("abstractbasepage_ptr")
        new_contactfield.model = CoyoteHomePage
        new_contactfield.contribute_to_class(CoyoteHomePage, "abstractbasepage_ptr")

        schema_editor.alter_field(
            CoyoteContactPage,
            CoyoteContactPage._meta.get_field("abstractbasepage_ptr"),
            new_contactfield,
            strict=True,
        )
        schema_editor.alter_field(
            CoyoteHomePage,
            CoyoteHomePage._meta.get_field("abstractbasepage_ptr"),
            new_homepagefield,
            strict=True,
        )


class Migration(migrations.Migration):
    dependencies = [
        ("coyote", "0023_auto_20250929_1604"),
        ("ocyan_plugin_wagtail", "0029_alter_abstractbasepage_canonical_url"),
    ]

    operations = [
        migrations.RunPython(drop_page_ptr, migrations.RunPython.noop),
    ]
