"""platform_stat_display table (首页三统计展示配置:每指标独立 real/manual/random)

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

播种 3 行:默认 manual + 客户端原写死门面值(12847 人 / 86532 次 / 37621.4 元),
保证上线前后展示一致,运营再按需切模式。
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'pstat1d2e3f4a'
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:
    table = op.create_table(
        'platform_stat_display',
        sa.Column('metric', sa.String(length=32), nullable=False),
        sa.Column('mode', sa.String(length=16), nullable=False),
        sa.Column('manual_value', sa.Integer(), nullable=True),
        sa.Column('random_mult_min', sa.Integer(), nullable=False),
        sa.Column('random_mult_max', sa.Integer(), nullable=False),
        sa.Column('random_tick_seconds', sa.Integer(), nullable=False),
        sa.Column('random_current', sa.Integer(), nullable=True),
        sa.Column('random_last_tick_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_by_admin_id', sa.Integer(), nullable=True),
        sa.Column(
            'updated_at', sa.DateTime(timezone=True),
            server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False,
        ),
        sa.PrimaryKeyConstraint('metric'),
    )

    _common = {
        'mode': 'manual',
        'random_mult_min': 1000,  # 1.000(只增不减下限)
        'random_mult_max': 1100,  # 1.100
        'random_tick_seconds': 86400,  # 1 天
    }
    op.bulk_insert(
        table,
        [
            {'metric': 'help_users', 'manual_value': 12847, **_common},
            {'metric': 'total_compares', 'manual_value': 86532, **_common},
            {'metric': 'total_saved', 'manual_value': 3762140, **_common},  # 37621.40 元
        ],
    )


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