"""add menu item options and order/cart item customization

Revision ID: 8625b9cb6638
Revises: 42828b3f2eea
Create Date: 2026-07-16 15:35:15.519082

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "8625b9cb6638"
down_revision: Union[str, None] = "42828b3f2eea"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "menu_item_option_groups",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("menu_item_id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("selection_type", sa.String(length=20), nullable=False),
        sa.Column("is_required", sa.Boolean(), nullable=False),
        sa.Column("display_order", sa.Integer(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column(
            "updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["menu_item_id"], ["menu_items.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_menu_item_option_groups_menu_item_id"),
        "menu_item_option_groups",
        ["menu_item_id"],
        unique=False,
    )
    op.create_table(
        "menu_item_options",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("group_id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("price_delta", sa.Numeric(precision=10, scale=2), nullable=False),
        sa.Column("display_order", sa.Integer(), nullable=False),
        sa.Column("is_available", sa.Boolean(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column(
            "updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["group_id"], ["menu_item_option_groups.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_menu_item_options_group_id"),
        "menu_item_options",
        ["group_id"],
        unique=False,
    )
    op.create_table(
        "order_item_options",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("order_item_id", sa.Uuid(), nullable=False),
        sa.Column("name_snapshot", sa.String(length=100), nullable=False),
        sa.Column(
            "price_delta_snapshot", sa.Numeric(precision=10, scale=2), nullable=False
        ),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["order_item_id"], ["order_items.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_order_item_options_order_item_id"),
        "order_item_options",
        ["order_item_id"],
        unique=False,
    )
    op.create_table(
        "cart_item_options",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("cart_item_id", sa.Uuid(), nullable=False),
        sa.Column("menu_item_option_id", sa.Uuid(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["cart_item_id"], ["cart_items.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["menu_item_option_id"], ["menu_item_options.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("cart_item_id", "menu_item_option_id"),
    )
    op.create_index(
        op.f("ix_cart_item_options_cart_item_id"),
        "cart_item_options",
        ["cart_item_id"],
        unique=False,
    )
    # NOTE: autogenerate also proposed op.drop_table('spatial_ref_sys') (a
    # PostGIS system table — dropping it would break PostGIS entirely) and
    # dropping refresh_tokens_token_hash_key/users_phone_key (pre-existing
    # unique-index-vs-named-constraint drift unrelated to this migration).
    # Both removed by hand — same drift called out in 42828b3f2eea.
    op.add_column(
        "cart_items",
        sa.Column("special_instructions", sa.String(length=500), nullable=True),
    )
    op.drop_constraint(
        "cart_items_cart_id_menu_item_id_key", "cart_items", type_="unique"
    )
    op.add_column(
        "order_items",
        sa.Column("special_instructions", sa.String(length=500), nullable=True),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("order_items", "special_instructions")
    op.create_unique_constraint(
        "cart_items_cart_id_menu_item_id_key",
        "cart_items",
        ["cart_id", "menu_item_id"],
        postgresql_nulls_not_distinct=False,
    )
    op.drop_column("cart_items", "special_instructions")
    op.drop_index(
        op.f("ix_cart_item_options_cart_item_id"), table_name="cart_item_options"
    )
    op.drop_table("cart_item_options")
    op.drop_index(
        op.f("ix_order_item_options_order_item_id"), table_name="order_item_options"
    )
    op.drop_table("order_item_options")
    op.drop_index(op.f("ix_menu_item_options_group_id"), table_name="menu_item_options")
    op.drop_table("menu_item_options")
    op.drop_index(
        op.f("ix_menu_item_option_groups_menu_item_id"),
        table_name="menu_item_option_groups",
    )
    op.drop_table("menu_item_option_groups")
    # ### end Alembic commands ###
