"""add user_tokens table for email verification and password reset

Revision ID: e7a936dc8024
Revises: 2bcbf00568eb
Create Date: 2025-09-22 00:17:27.129384

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "e7a936dc8024"
down_revision = "2bcbf00568eb"
branch_labels = None
depends_on = None


def upgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.create_table(
    "user_tokens",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("token_hash", sa.String(), nullable=False),
    sa.Column("token_type", sa.String(length=50), nullable=False),
    sa.Column("expires_at", sa.DateTime(), nullable=False),
    sa.Column("used_at", sa.DateTime(), nullable=True),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("ip_address", sa.String(length=45), nullable=True),
    sa.Column("user_agent", sa.Text(), nullable=True),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_expired_unused", "user_tokens", ["expires_at", "used_at"], unique=False
  )
  op.create_index("idx_expires", "user_tokens", ["expires_at"], unique=False)
  op.create_index(
    "idx_user_tokens", "user_tokens", ["user_id", "token_type"], unique=False
  )
  op.create_index(
    op.f("ix_user_tokens_token_hash"), "user_tokens", ["token_hash"], unique=True
  )
  op.create_index(
    op.f("ix_user_tokens_user_id"), "user_tokens", ["user_id"], unique=False
  )
  # ### end Alembic commands ###


def downgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_index(op.f("ix_user_tokens_user_id"), table_name="user_tokens")
  op.drop_index(op.f("ix_user_tokens_token_hash"), table_name="user_tokens")
  op.drop_index("idx_user_tokens", table_name="user_tokens")
  op.drop_index("idx_expires", table_name="user_tokens")
  op.drop_index("idx_expired_unused", table_name="user_tokens")
  op.drop_table("user_tokens")
  # ### end Alembic commands ###
