"""Fix content_page nullable boolean columns

Revision ID: z7h8i9j0k1l2
Revises: z6g7h8i9j0k1
Create Date: 2026-01-20

This migration:
1. Sets NULL values to defaults for boolean and integer columns
2. Alters columns to be NOT NULL
"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "z7h8i9j0k1l2"
down_revision = "z6g7h8i9j0k1"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # First, update any NULL values to defaults
    op.execute("""
        UPDATE content_pages
        SET display_order = 0
        WHERE display_order IS NULL
    """)

    op.execute("""
        UPDATE content_pages
        SET show_in_footer = true
        WHERE show_in_footer IS NULL
    """)

    op.execute("""
        UPDATE content_pages
        SET show_in_header = false
        WHERE show_in_header IS NULL
    """)

    op.execute("""
        UPDATE content_pages
        SET show_in_legal = false
        WHERE show_in_legal IS NULL
    """)

    # Now alter columns to be NOT NULL
    op.alter_column(
        "content_pages",
        "display_order",
        existing_type=sa.Integer(),
        nullable=False,
        server_default="0",
    )

    op.alter_column(
        "content_pages",
        "show_in_footer",
        existing_type=sa.Boolean(),
        nullable=False,
        server_default="true",
    )

    op.alter_column(
        "content_pages",
        "show_in_header",
        existing_type=sa.Boolean(),
        nullable=False,
        server_default="false",
    )

    op.alter_column(
        "content_pages",
        "show_in_legal",
        existing_type=sa.Boolean(),
        nullable=False,
        server_default="false",
    )


def downgrade() -> None:
    # Revert columns to nullable (no server default)
    op.alter_column(
        "content_pages",
        "display_order",
        existing_type=sa.Integer(),
        nullable=True,
        server_default=None,
    )

    op.alter_column(
        "content_pages",
        "show_in_footer",
        existing_type=sa.Boolean(),
        nullable=True,
        server_default=None,
    )

    op.alter_column(
        "content_pages",
        "show_in_header",
        existing_type=sa.Boolean(),
        nullable=True,
        server_default=None,
    )

    op.alter_column(
        "content_pages",
        "show_in_legal",
        existing_type=sa.Boolean(),
        nullable=True,
        server_default=None,
    )
