"""replace post_id FKs with container_type + container_id

Revision ID: g7e5b1c3d4f8
Revises: f6d4a0b2c3e7
Create Date: 2026-02-11
"""
from alembic import op
import sqlalchemy as sa

revision = 'g7e5b1c3d4f8'
down_revision = 'f6d4a0b2c3e7'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- calendars: post_id → container_type + container_id ---
    op.add_column('calendars', sa.Column('container_type', sa.String(32), nullable=True))
    op.add_column('calendars', sa.Column('container_id', sa.Integer(), nullable=True))
    op.execute("UPDATE calendars SET container_type = 'page', container_id = post_id")
    op.alter_column('calendars', 'container_type', nullable=False, server_default=sa.text("'page'"))
    op.alter_column('calendars', 'container_id', nullable=False)
    op.drop_index('ix_calendars_post_id', table_name='calendars')
    op.drop_index('ux_calendars_post_slug_active', table_name='calendars')
    op.drop_constraint('calendars_post_id_fkey', 'calendars', type_='foreignkey')
    op.drop_column('calendars', 'post_id')
    op.create_index('ix_calendars_container', 'calendars', ['container_type', 'container_id'])
    op.create_index(
        'ux_calendars_container_slug_active',
        'calendars',
        ['container_type', 'container_id', sa.text('lower(slug)')],
        unique=True,
        postgresql_where=sa.text('deleted_at IS NULL'),
    )

    # --- market_places: post_id → container_type + container_id ---
    op.add_column('market_places', sa.Column('container_type', sa.String(32), nullable=True))
    op.add_column('market_places', sa.Column('container_id', sa.Integer(), nullable=True))
    op.execute("UPDATE market_places SET container_type = 'page', container_id = post_id")
    op.alter_column('market_places', 'container_type', nullable=False, server_default=sa.text("'page'"))
    op.alter_column('market_places', 'container_id', nullable=False)
    op.drop_index('ix_market_places_post_id', table_name='market_places')
    op.drop_constraint('market_places_post_id_fkey', 'market_places', type_='foreignkey')
    op.drop_column('market_places', 'post_id')
    op.create_index('ix_market_places_container', 'market_places', ['container_type', 'container_id'])

    # --- page_configs: post_id → container_type + container_id ---
    op.add_column('page_configs', sa.Column('container_type', sa.String(32), nullable=True))
    op.add_column('page_configs', sa.Column('container_id', sa.Integer(), nullable=True))
    op.execute("UPDATE page_configs SET container_type = 'page', container_id = post_id")
    op.alter_column('page_configs', 'container_type', nullable=False, server_default=sa.text("'page'"))
    op.alter_column('page_configs', 'container_id', nullable=False)
    op.drop_constraint('page_configs_post_id_fkey', 'page_configs', type_='foreignkey')
    op.drop_column('page_configs', 'post_id')
    op.create_index('ix_page_configs_container', 'page_configs', ['container_type', 'container_id'])

    # --- calendar_entry_posts: post_id → content_type + content_id ---
    op.add_column('calendar_entry_posts', sa.Column('content_type', sa.String(32), nullable=True))
    op.add_column('calendar_entry_posts', sa.Column('content_id', sa.Integer(), nullable=True))
    op.execute("UPDATE calendar_entry_posts SET content_type = 'post', content_id = post_id")
    op.alter_column('calendar_entry_posts', 'content_type', nullable=False, server_default=sa.text("'post'"))
    op.alter_column('calendar_entry_posts', 'content_id', nullable=False)
    op.drop_index('ix_entry_posts_post_id', table_name='calendar_entry_posts')
    op.drop_constraint('calendar_entry_posts_post_id_fkey', 'calendar_entry_posts', type_='foreignkey')
    op.drop_column('calendar_entry_posts', 'post_id')
    op.create_index('ix_entry_posts_content', 'calendar_entry_posts', ['content_type', 'content_id'])


def downgrade() -> None:
    # --- calendar_entry_posts: restore post_id ---
    op.add_column('calendar_entry_posts', sa.Column('post_id', sa.Integer(), nullable=True))
    op.execute("UPDATE calendar_entry_posts SET post_id = content_id WHERE content_type = 'post'")
    op.alter_column('calendar_entry_posts', 'post_id', nullable=False)
    op.create_foreign_key('calendar_entry_posts_post_id_fkey', 'calendar_entry_posts', 'posts', ['post_id'], ['id'], ondelete='CASCADE')
    op.create_index('ix_entry_posts_post_id', 'calendar_entry_posts', ['post_id'])
    op.drop_index('ix_entry_posts_content', table_name='calendar_entry_posts')
    op.drop_column('calendar_entry_posts', 'content_id')
    op.drop_column('calendar_entry_posts', 'content_type')

    # --- page_configs: restore post_id ---
    op.add_column('page_configs', sa.Column('post_id', sa.Integer(), nullable=True))
    op.execute("UPDATE page_configs SET post_id = container_id WHERE container_type = 'page'")
    op.alter_column('page_configs', 'post_id', nullable=False)
    op.create_foreign_key('page_configs_post_id_fkey', 'page_configs', 'posts', ['post_id'], ['id'], ondelete='CASCADE')
    op.drop_index('ix_page_configs_container', table_name='page_configs')
    op.drop_column('page_configs', 'container_id')
    op.drop_column('page_configs', 'container_type')

    # --- market_places: restore post_id ---
    op.add_column('market_places', sa.Column('post_id', sa.Integer(), nullable=True))
    op.execute("UPDATE market_places SET post_id = container_id WHERE container_type = 'page'")
    op.alter_column('market_places', 'post_id', nullable=False)
    op.create_foreign_key('market_places_post_id_fkey', 'market_places', 'posts', ['post_id'], ['id'], ondelete='CASCADE')
    op.create_index('ix_market_places_post_id', 'market_places', ['post_id'])
    op.drop_index('ix_market_places_container', table_name='market_places')
    op.drop_column('market_places', 'container_id')
    op.drop_column('market_places', 'container_type')

    # --- calendars: restore post_id ---
    op.add_column('calendars', sa.Column('post_id', sa.Integer(), nullable=True))
    op.execute("UPDATE calendars SET post_id = container_id WHERE container_type = 'page'")
    op.alter_column('calendars', 'post_id', nullable=False)
    op.create_foreign_key('calendars_post_id_fkey', 'calendars', 'posts', ['post_id'], ['id'], ondelete='CASCADE')
    op.create_index('ix_calendars_post_id', 'calendars', ['post_id'])
    op.create_index(
        'ux_calendars_post_slug_active',
        'calendars',
        ['post_id', sa.text('lower(slug)')],
        unique=True,
        postgresql_where=sa.text('deleted_at IS NULL'),
    )
    op.drop_index('ux_calendars_container_slug_active', table_name='calendars')
    op.drop_index('ix_calendars_container', table_name='calendars')
    op.drop_column('calendars', 'container_id')
    op.drop_column('calendars', 'container_type')
