"""add reset_token and reset_token_expires to users

Revision ID: a1b2c3d4e5f6
Revises: 5206607942a2
Create Date: 2026-05-01 10:00:00.000000
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    op.add_column("users", sa.Column("reset_token", sa.String(length=255), nullable=True))
    op.add_column("users", sa.Column("reset_token_expires", sa.DateTime(), nullable=True))
    # Make legacy password_hash column nullable (ORM uses hashed_password now).
    # Use batch_alter_table for SQLite compatibility.
    with op.batch_alter_table("users") as batch_op:
        batch_op.alter_column("password_hash", nullable=True)


def downgrade() -> None:
    with op.batch_alter_table("users") as batch_op:
        batch_op.drop_column("reset_token_expires")
        batch_op.drop_column("reset_token")
        batch_op.alter_column("password_hash", nullable=False)
