"""store_mapping table (平台店铺表:跨平台同店 id/名 映射,server 侧无条件落库)

Revision ID: store_mapping_table
Revises: coin_txn_task_ref_uq
Create Date: 2026-06-13 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'store_mapping_table'
down_revision: Union[str, Sequence[str], None] = 'coin_txn_task_ref_uq'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        'store_mapping',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        # 跨平台身份
        sa.Column('id_taobao', sa.String(length=64), nullable=True),
        sa.Column('name_taobao', sa.String(length=128), nullable=True),
        sa.Column('id_meituan', sa.String(length=64), nullable=True),
        sa.Column('name_meituan', sa.String(length=128), nullable=True),
        sa.Column('id_jd', sa.String(length=64), nullable=True),
        sa.Column('name_jd', sa.String(length=128), nullable=True),
        # 地理
        sa.Column('city', sa.String(length=64), nullable=True),
        sa.Column('geohash', sa.String(length=16), nullable=True),
        sa.Column('lng', sa.Float(), nullable=True),
        sa.Column('lat', sa.Float(), nullable=True),
        sa.Column('taobao_address', sa.String(length=256), nullable=True),
        # 溯源
        sa.Column('source_platform', sa.String(length=32), nullable=True),
        sa.Column('business_type', sa.String(length=16), nullable=False),
        sa.Column('trace_id', sa.String(length=64), nullable=False),
        sa.Column('source_device_id', sa.String(length=64), nullable=True),
        sa.Column('source_user_id', sa.Integer(), nullable=True),
        # 淘宝原料(URL 可能很长 → Text)
        sa.Column('taobao_share_url', sa.String(length=256), nullable=True),
        sa.Column('taobao_resolved_url', sa.Text(), nullable=True),
        sa.Column('taobao_deeplink', sa.Text(), nullable=True),
        # PG 上为 JSONB,其它(SQLite)为 JSON——与模型层 with_variant 对齐
        sa.Column('attrs', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        # 一次比价一行,trace_id 幂等去重(防 pricebot 重试 / replay 重复写)
        sa.UniqueConstraint('trace_id', name='uq_store_mapping_trace'),
    )
    with op.batch_alter_table('store_mapping', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_store_mapping_id_taobao'), ['id_taobao'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_id_meituan'), ['id_meituan'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_id_jd'), ['id_jd'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_geohash'), ['geohash'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_source_platform'), ['source_platform'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_source_device_id'), ['source_device_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_source_user_id'), ['source_user_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_store_mapping_created_at'), ['created_at'], unique=False)


def downgrade() -> None:
    with op.batch_alter_table('store_mapping', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_store_mapping_created_at'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_source_user_id'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_source_device_id'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_source_platform'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_geohash'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_id_jd'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_id_meituan'))
        batch_op.drop_index(batch_op.f('ix_store_mapping_id_taobao'))

    op.drop_table('store_mapping')
