"""Drop unique constraint on conversation(restaurant_id, identity_key).

Multiple conversations per user are needed to track history across sessions.
When a conversation is ended (e.g. via the dashboard "forget" toggle), the
next inbound message must be able to create a fresh conversation row.

Revision ID: 0025_drop_conversation_unique_constraint
Revises: 0024_add_conversation_context_enabled
Create Date: 2026-03-20 16:00:00.000000

"""

from collections.abc import Sequence

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0025_drop_conversation_unique_constraint"
down_revision: str = "0024_add_conversation_context_enabled"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.drop_constraint(
        "uq_conversation_restaurant_identity_key",
        "conversation",
        type_="unique",
    )
    # Keep a non-unique index for fast lookups by identity_key.
    op.create_index(
        "ix_conversation_restaurant_identity_key",
        "conversation",
        ["restaurant_id", "identity_key"],
    )


def downgrade() -> None:
    op.drop_index("ix_conversation_restaurant_identity_key", table_name="conversation")
    op.create_unique_constraint(
        "uq_conversation_restaurant_identity_key",
        "conversation",
        ["restaurant_id", "identity_key"],
    )
