"""meituan_coupon table(美团 CPS 券本地缓存,供销量/佣金排序从库里捞、本地排序)

Revision ID: meituan_coupon_table
Revises: withdraw_review_ad_watch
Create Date: 2026-06-06 12:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql


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


def upgrade() -> None:
    op.create_table(
        'meituan_coupon',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('source', sa.String(length=16), nullable=False),
        sa.Column('platform', sa.Integer(), nullable=False),
        sa.Column('biz_line', sa.Integer(), nullable=True),
        sa.Column('city_id', sa.String(length=32), nullable=False),
        sa.Column('product_view_sign', sa.String(length=128), nullable=False),
        sa.Column('sku_view_id', sa.String(length=128), nullable=True),
        sa.Column('name', sa.String(length=256), nullable=True),
        sa.Column('brand_name', sa.String(length=128), nullable=True),
        sa.Column('sell_price_cents', sa.Integer(), nullable=True),
        sa.Column('original_price_cents', sa.Integer(), nullable=True),
        sa.Column('head_url', sa.String(length=512), nullable=True),
        sa.Column('sale_volume', sa.String(length=32), nullable=True),
        sa.Column('sale_volume_num', sa.Integer(), nullable=True),
        sa.Column('commission_percent', sa.Float(), nullable=True),
        sa.Column('commission_amount_cents', sa.Integer(), nullable=True),
        sa.Column('poi_name', sa.String(length=128), nullable=True),
        sa.Column('available_poi_num', sa.Integer(), nullable=True),
        sa.Column('delivery_distance_m', sa.Float(), nullable=True),
        sa.Column('dedup_key', sa.String(length=64), nullable=False),
        sa.Column('raw', sa.JSON().with_variant(postgresql.JSONB(), 'postgresql'), nullable=False),
        sa.Column('first_seen', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.Column('last_seen', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('source', 'product_view_sign', name='uq_meituan_coupon_source_sign'),
    )
    op.create_index('ix_meituan_coupon_source', 'meituan_coupon', ['source'])
    op.create_index('ix_meituan_coupon_city_id', 'meituan_coupon', ['city_id'])
    op.create_index('ix_meituan_coupon_brand_name', 'meituan_coupon', ['brand_name'])
    op.create_index('ix_meituan_coupon_sale_volume_num', 'meituan_coupon', ['sale_volume_num'])
    op.create_index('ix_meituan_coupon_commission_percent', 'meituan_coupon', ['commission_percent'])
    op.create_index('ix_meituan_coupon_dedup_key', 'meituan_coupon', ['dedup_key'])
    op.create_index('ix_meituan_coupon_last_seen', 'meituan_coupon', ['last_seen'])


def downgrade() -> None:
    op.drop_table('meituan_coupon')
