"""welfare tables: coin account, coin txn, signin, user task

Revision ID: 357520ea3015
Revises: 9c559acc164a
Create Date: 2026-05-24 16:16:20.677711

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '357520ea3015'
down_revision: Union[str, Sequence[str], None] = '9c559acc164a'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('coin_account',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('coin_balance', sa.Integer(), nullable=False),
    sa.Column('cash_balance_cents', sa.Integer(), nullable=False),
    sa.Column('total_coin_earned', sa.Integer(), 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('user_id')
    )
    op.create_table('coin_transaction',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('amount', sa.Integer(), nullable=False),
    sa.Column('balance_after', sa.Integer(), nullable=False),
    sa.Column('biz_type', sa.String(length=32), nullable=False),
    sa.Column('ref_id', sa.String(length=64), nullable=True),
    sa.Column('remark', sa.String(length=128), 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')
    )
    with op.batch_alter_table('coin_transaction', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_coin_transaction_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_coin_transaction_user_id'), ['user_id'], unique=False)

    op.create_table('signin_record',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('signin_date', sa.Date(), nullable=False),
    sa.Column('cycle_day', sa.Integer(), nullable=False),
    sa.Column('streak', sa.Integer(), nullable=False),
    sa.Column('coin_awarded', sa.Integer(), nullable=False),
    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', 'signin_date', name='uq_signin_user_date')
    )
    with op.batch_alter_table('signin_record', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_signin_record_user_id'), ['user_id'], unique=False)

    op.create_table('user_task',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('task_key', sa.String(length=48), nullable=False),
    sa.Column('status', sa.String(length=16), nullable=False),
    sa.Column('coin_awarded', sa.Integer(), nullable=False),
    sa.Column('completed_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', 'task_key', name='uq_task_user_key')
    )
    with op.batch_alter_table('user_task', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_user_task_user_id'), ['user_id'], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user_task', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_user_task_user_id'))

    op.drop_table('user_task')
    with op.batch_alter_table('signin_record', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_signin_record_user_id'))

    op.drop_table('signin_record')
    with op.batch_alter_table('coin_transaction', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_coin_transaction_user_id'))
        batch_op.drop_index(batch_op.f('ix_coin_transaction_created_at'))

    op.drop_table('coin_transaction')
    op.drop_table('coin_account')
    # ### end Alembic commands ###
