"""add cart_items table

Revision ID: a2064e1dfcd4
Revises: f68d8da5315a
Create Date: 2025-11-23 19:52:40.509538

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "a2064e1dfcd4"
down_revision: str | None = "f68d8da5315a"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Create cart_items table
    op.create_table(
        "cart_items",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("vendor_id", sa.Integer(), nullable=False),
        sa.Column("product_id", sa.Integer(), nullable=False),
        sa.Column("session_id", sa.String(length=255), nullable=False),
        sa.Column("quantity", sa.Integer(), nullable=False),
        sa.Column("price_at_add", sa.Float(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["product_id"],
            ["products.id"],
        ),
        sa.ForeignKeyConstraint(
            ["vendor_id"],
            ["vendors.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "vendor_id", "session_id", "product_id", name="uq_cart_item"
        ),
    )

    # Create indexes
    op.create_index(
        "idx_cart_session", "cart_items", ["vendor_id", "session_id"], unique=False
    )
    op.create_index("idx_cart_created", "cart_items", ["created_at"], unique=False)
    op.create_index(op.f("ix_cart_items_id"), "cart_items", ["id"], unique=False)
    op.create_index(
        op.f("ix_cart_items_session_id"), "cart_items", ["session_id"], unique=False
    )


def downgrade() -> None:
    # Drop indexes
    op.drop_index(op.f("ix_cart_items_session_id"), table_name="cart_items")
    op.drop_index(op.f("ix_cart_items_id"), table_name="cart_items")
    op.drop_index("idx_cart_created", table_name="cart_items")
    op.drop_index("idx_cart_session", table_name="cart_items")

    # Drop table
    op.drop_table("cart_items")
