"""MPS-20 + MPS-21: cart-auction + cart-offer association tables

Revision ID: 34e1c65d0bea
Revises: f0213a54cf7b
Create Date: 2026-05-09 20:10:46.799472

Adds two association tables that override Cart.total_in_cents:
- mps_cart_auction links a cart to a winning auction; cart total uses
  the auction's winning bid amount instead of product list price.
- mps_cart_offer links a cart to an accepted offer; cart total uses
  the offer's current_amount_in_cents instead of product list price.

Idempotent — make init-db creates tables from models.
"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


revision = '34e1c65d0bea'
down_revision = 'f0213a54cf7b'
branch_labels = None
depends_on = None


def _table_exists(name):
    conn = op.get_bind()
    result = conn.execute(
        sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name=:name"),
        {"name": name},
    )
    return result.fetchone() is not None


UUID = sqlalchemy_utils.types.uuid.UUIDType(binary=False)


def upgrade():
    if not _table_exists("mps_cart_auction"):
        op.create_table(
            "mps_cart_auction",
            sa.Column("id", UUID, nullable=False),
            sa.Column("cart_id", UUID, nullable=False),
            sa.Column("auction_id", UUID, nullable=False),
            sa.Column("created_timestamp", sa.BigInteger(), nullable=False),
            sa.ForeignKeyConstraint(["auction_id"], ["mps_auction.id"]),
            sa.ForeignKeyConstraint(["cart_id"], ["mps_cart.id"]),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index("ix_mps_cart_auction_id", "mps_cart_auction", ["id"])
        op.create_index(
            "ix_mps_cart_auction_auction_id", "mps_cart_auction", ["auction_id"],
        )

    if not _table_exists("mps_cart_offer"):
        op.create_table(
            "mps_cart_offer",
            sa.Column("id", UUID, nullable=False),
            sa.Column("cart_id", UUID, nullable=False),
            sa.Column("offer_id", UUID, nullable=False),
            sa.Column("created_timestamp", sa.BigInteger(), nullable=False),
            sa.ForeignKeyConstraint(["cart_id"], ["mps_cart.id"]),
            sa.ForeignKeyConstraint(["offer_id"], ["mps_offer.id"]),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index("ix_mps_cart_offer_id", "mps_cart_offer", ["id"])
        op.create_index(
            "ix_mps_cart_offer_offer_id", "mps_cart_offer", ["offer_id"],
        )


def downgrade():
    if _table_exists("mps_cart_offer"):
        op.drop_index("ix_mps_cart_offer_offer_id", table_name="mps_cart_offer")
        op.drop_index("ix_mps_cart_offer_id", table_name="mps_cart_offer")
        op.drop_table("mps_cart_offer")
    if _table_exists("mps_cart_auction"):
        op.drop_index(
            "ix_mps_cart_auction_auction_id", table_name="mps_cart_auction",
        )
        op.drop_index("ix_mps_cart_auction_id", table_name="mps_cart_auction")
        op.drop_table("mps_cart_auction")
