"""Add page_configs table — moved from cart service to blog.

Revision ID: blog_0003
Revises: blog_0002
Create Date: 2026-02-27
"""

import sqlalchemy as sa
from alembic import op

revision = "blog_0003"
down_revision = "blog_0002"
branch_labels = None
depends_on = None


def _table_exists(conn, name):
    result = conn.execute(sa.text(
        "SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name=:t"
    ), {"t": name})
    return result.scalar() is not None


def upgrade():
    if _table_exists(op.get_bind(), "page_configs"):
        return

    op.create_table(
        "page_configs",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("container_type", sa.String(32), nullable=False, server_default=sa.text("'page'")),
        sa.Column("container_id", sa.Integer, nullable=False),
        sa.Column("features", sa.JSON, nullable=False, server_default="{}"),
        sa.Column("sumup_merchant_code", sa.String(64), nullable=True),
        sa.Column("sumup_api_key", sa.Text, nullable=True),
        sa.Column("sumup_checkout_prefix", sa.String(64), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )


def downgrade():
    op.drop_table("page_configs")
