"""Rename remaining vendor-named constraints and indexes to store.

Revision ID: t002_constraints
Revises: t001_terminology
Create Date: 2026-02-07 10:00:00.000000

Completes the Company/Vendor -> Merchant/Store terminology migration by
renaming 4 constraints and 12 indexes that still used "vendor" in their names.
"""

from collections.abc import Sequence

from alembic import op

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

# (old_name, new_name, table) — table is needed for RENAME CONSTRAINT
CONSTRAINTS = [
    ("uq_vendor_marketplace_product", "uq_store_marketplace_product", "products"),
    ("uq_vendor_platform", "uq_store_platform", "store_platforms"),
    ("uq_vendor_domain", "uq_store_domain", "store_domains"),
    ("uq_vendor_email_template_code_language", "uq_store_email_template_code_language", "store_email_templates"),
]

# (old_name, new_name) — ALTER INDEX works without table name in PostgreSQL
INDEXES = [
    ("idx_product_vendor_active", "idx_product_store_active"),
    ("idx_product_vendor_featured", "idx_product_store_featured"),
    ("idx_product_vendor_sku", "idx_product_store_sku"),
    ("idx_vendor_platform_active", "idx_store_platform_active"),
    ("idx_vendor_platform_primary", "idx_store_platform_primary"),
    ("idx_vendor_primary", "idx_store_domain_primary"),
    ("idx_vendor_cache_city", "idx_letzshop_cache_city"),
    ("idx_vendor_cache_claimed", "idx_letzshop_cache_claimed"),
    ("idx_vendor_cache_active", "idx_letzshop_cache_active"),
    ("idx_vendor_addon_status", "idx_store_addon_status"),
    ("idx_vendor_addon_product", "idx_store_addon_product"),
    ("idx_vendor_email_settings_configured", "idx_store_email_settings_configured"),
]


def upgrade() -> None:
    for old, new, table in CONSTRAINTS:
        op.execute(f'ALTER TABLE "{table}" RENAME CONSTRAINT "{old}" TO "{new}"')

    for old, new in INDEXES:
        op.execute(f'ALTER INDEX "{old}" RENAME TO "{new}"')


def downgrade() -> None:
    # Reverse constraint renames
    for old, new, table in CONSTRAINTS:
        op.execute(f'ALTER TABLE "{table}" RENAME CONSTRAINT "{new}" TO "{old}"')

    # Reverse index renames
    for old, new in INDEXES:
        op.execute(f'ALTER INDEX "{new}" RENAME TO "{old}"')
