"""Add container_relations table to cart DB.

Generic parent/child graph for inter-service container relationships
(page→market, page→calendar, page→menu_node, etc.).

Revision ID: cart_0003
Revises: cart_0002
Create Date: 2026-02-26
"""

import sqlalchemy as sa
from alembic import op

revision = "cart_0003"
down_revision = "cart_0002"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "container_relations",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("parent_type", sa.String(32), nullable=False),
        sa.Column("parent_id", sa.Integer, nullable=False),
        sa.Column("child_type", sa.String(32), nullable=False),
        sa.Column("child_id", sa.Integer, nullable=False),
        sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"),
        sa.Column("label", sa.String(255), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.UniqueConstraint("parent_type", "parent_id", "child_type", "child_id",
                            name="uq_container_relations_parent_child"),
    )
    op.create_index("ix_container_relations_parent", "container_relations", ["parent_type", "parent_id"])
    op.create_index("ix_container_relations_child", "container_relations", ["child_type", "child_id"])


def downgrade() -> None:
    op.drop_table("container_relations")
