"""comparison_record table (比价记录:每次比价完整明细,「我的比价记录」数据源)

Revision ID: b2c3d4e5f6a7
Revises: a1b2c3d4e5f6
Create Date: 2026-05-31 15:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    op.create_table(
        'comparison_record',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('device_id', sa.String(length=64), 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_platform_id', sa.String(length=32), nullable=True),
        sa.Column('source_platform_name', sa.String(length=32), nullable=True),
        sa.Column('source_package', sa.String(length=128), nullable=True),
        sa.Column('source_price_cents', sa.Integer(), nullable=True),
        sa.Column('best_platform_id', sa.String(length=32), nullable=True),
        sa.Column('best_platform_name', sa.String(length=32), nullable=True),
        sa.Column('best_price_cents', sa.Integer(), nullable=True),
        sa.Column('saved_amount_cents', sa.Integer(), nullable=True),
        sa.Column('is_source_best', sa.Boolean(), nullable=True),
        sa.Column('store_name', sa.String(length=128), nullable=True),
        sa.Column('total_dish_count', sa.Integer(), nullable=True),
        sa.Column('skipped_dish_count', sa.Integer(), nullable=True),
        sa.Column('status', sa.String(length=16), nullable=False),
        # PG 上为 JSONB,其它(SQLite)为 JSON——与模型层 with_variant 对齐
        sa.Column('items', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=False),
        sa.Column('comparison_results', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=False),
        sa.Column('skipped_dish_names', sa.JSON().with_variant(sa.dialects.postgresql.JSONB(), 'postgresql'), nullable=False),
        sa.Column('raw_payload', 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.ForeignKeyConstraint(['user_id'], ['user.id'], ),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('user_id', 'trace_id', name='uq_comparison_user_trace'),
    )
    with op.batch_alter_table('comparison_record', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_comparison_record_user_id'), ['user_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_comparison_record_business_type'), ['business_type'], unique=False)
        batch_op.create_index(batch_op.f('ix_comparison_record_created_at'), ['created_at'], unique=False)


def downgrade() -> None:
    with op.batch_alter_table('comparison_record', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_comparison_record_created_at'))
        batch_op.drop_index(batch_op.f('ix_comparison_record_business_type'))
        batch_op.drop_index(batch_op.f('ix_comparison_record_user_id'))

    op.drop_table('comparison_record')
