"""add favorites

Revision ID: 4e8dc47a5e4c
Revises: e972896dc833
Create Date: 2026-07-22 16:37:27.327504

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "4e8dc47a5e4c"
down_revision: Union[str, None] = "e972896dc833"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 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 (recurring,
    # pre-existing unique-index-vs-named-constraint drift unrelated to
    # this migration — same drift called out in 42828b3f2eea). Both
    # removed by hand.
    op.create_table(
        "favorites",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("customer_id", sa.Uuid(), nullable=False),
        sa.Column("branch_id", sa.Uuid(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(["branch_id"], ["branches.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["customer_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("customer_id", "branch_id"),
    )
    op.create_index(
        op.f("ix_favorites_branch_id"), "favorites", ["branch_id"], unique=False
    )
    op.create_index(
        op.f("ix_favorites_customer_id"), "favorites", ["customer_id"], unique=False
    )


def downgrade() -> None:
    op.drop_index(op.f("ix_favorites_customer_id"), table_name="favorites")
    op.drop_index(op.f("ix_favorites_branch_id"), table_name="favorites")
    op.drop_table("favorites")
