"""cart initial - cart items

Revision ID: cart_001
Revises: inventory_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "cart_001"
down_revision = "inventory_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- cart_items ---
    op.create_table(
        "cart_items",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id"), nullable=False),
        sa.Column("session_id", sa.String(255), nullable=False, index=True),
        sa.Column("quantity", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("price_at_add_cents", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("store_id", "session_id", "product_id", name="uq_cart_item"),
    )
    op.create_index("idx_cart_session", "cart_items", ["store_id", "session_id"])
    op.create_index("idx_cart_created", "cart_items", ["created_at"])


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