"""withdraw_order table + user.wechat_openid

Revision ID: e4a1c2b3d4f5
Revises: bf2a67c0e2ab
Create Date: 2026-05-25 16:20:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    op.create_table(
        'withdraw_order',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('out_bill_no', sa.String(length=64), nullable=False),
        sa.Column('amount_cents', sa.Integer(), nullable=False),
        sa.Column('status', sa.String(length=16), nullable=False),
        sa.Column('wechat_state', sa.String(length=32), nullable=True),
        sa.Column('transfer_bill_no', sa.String(length=64), nullable=True),
        sa.Column('package_info', sa.String(length=512), nullable=True),
        sa.Column('fail_reason', sa.String(length=256), nullable=True),
        sa.Column('created_at', 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.ForeignKeyConstraint(['user_id'], ['user.id'], ),
        sa.PrimaryKeyConstraint('id'),
    )
    with op.batch_alter_table('withdraw_order', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_withdraw_order_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_withdraw_order_user_id'), ['user_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_withdraw_order_out_bill_no'), ['out_bill_no'], unique=True)

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.add_column(sa.Column('wechat_openid', sa.String(length=64), nullable=True))


def downgrade() -> None:
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_column('wechat_openid')

    with op.batch_alter_table('withdraw_order', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_withdraw_order_out_bill_no'))
        batch_op.drop_index(batch_op.f('ix_withdraw_order_user_id'))
        batch_op.drop_index(batch_op.f('ix_withdraw_order_created_at'))

    op.drop_table('withdraw_order')
