"""price_observation table (价格观测:比价沉淀的价格资产层,server 侧无条件落库)

Revision ID: price_observation_table
Revises: wx_transfer_auth
Create Date: 2026-06-07 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    op.create_table(
        'price_observation',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('observed_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.Column('trace_id', sa.String(length=64), nullable=False),
        sa.Column('business_type', sa.String(length=16), nullable=False),
        sa.Column('platform', sa.String(length=32), nullable=False),
        sa.Column('platform_store_id', sa.String(length=64), nullable=True),
        sa.Column('store_name', 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('is_source', sa.Boolean(), nullable=False),
        sa.Column('scope', sa.String(length=16), nullable=False),
        sa.Column('price_cents', sa.Integer(), nullable=True),
        sa.Column('coupon_saved_cents', sa.Integer(), nullable=True),
        sa.Column('coupon_name', sa.String(length=64), nullable=True),
        sa.Column('store_closed', sa.String(length=32), nullable=True),
        sa.Column('rank', sa.Integer(), nullable=True),
        # PG 上为 JSONB,其它(SQLite)为 JSON——与模型层 with_variant 对齐
        sa.Column('dishes', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=True),
        sa.Column('attrs', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=True),
        sa.Column('source_device_id', sa.String(length=64), nullable=True),
        sa.Column('source_user_id', sa.Integer(), nullable=True),
        sa.Column('confidence', sa.Float(), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('trace_id', 'platform', 'scope', name='uq_price_obs_trace_platform_scope'),
    )
    with op.batch_alter_table('price_observation', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_price_observation_observed_at'), ['observed_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_trace_id'), ['trace_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_business_type'), ['business_type'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_platform'), ['platform'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_store_name'), ['store_name'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_geohash'), ['geohash'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_source_device_id'), ['source_device_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_price_observation_source_user_id'), ['source_user_id'], unique=False)


def downgrade() -> None:
    with op.batch_alter_table('price_observation', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_price_observation_source_user_id'))
        batch_op.drop_index(batch_op.f('ix_price_observation_source_device_id'))
        batch_op.drop_index(batch_op.f('ix_price_observation_geohash'))
        batch_op.drop_index(batch_op.f('ix_price_observation_store_name'))
        batch_op.drop_index(batch_op.f('ix_price_observation_platform'))
        batch_op.drop_index(batch_op.f('ix_price_observation_business_type'))
        batch_op.drop_index(batch_op.f('ix_price_observation_trace_id'))
        batch_op.drop_index(batch_op.f('ix_price_observation_observed_at'))

    op.drop_table('price_observation')
