"""Add temporary_registrations table (verify-first registration).

Holds a pending registration (name, email, hashed password, hashed OTP,
expiries) before any user account exists, so the ``users`` table never
accumulates unverified accounts. Rows are short-lived and deleted on successful
verification. No changes to existing tables.

Revision ID: f2b4d6e8a0c3
Revises: e1a3c5b7d9f2
Create Date: 2026-07-05

"""
from alembic import op
import sqlalchemy as sa


revision = "f2b4d6e8a0c3"
down_revision = "e1a3c5b7d9f2"
branch_labels = None
depends_on = None

_BIGINT = sa.BigInteger().with_variant(sa.Integer(), "sqlite")


def upgrade():
    op.create_table(
        "temporary_registrations",
        sa.Column("full_name", sa.String(length=150), nullable=False),
        sa.Column("email", sa.String(length=255), nullable=False),
        sa.Column("password_hash", sa.String(length=255), nullable=False),
        sa.Column("code_hash", sa.CHAR(length=64), nullable=False),
        sa.Column("code_expires_at", sa.DateTime(), nullable=False),
        sa.Column("expires_at", sa.DateTime(), nullable=False),
        sa.Column("resend_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_sent_at", sa.DateTime(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("id", _BIGINT, autoincrement=True, nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("temporary_registrations", schema=None) as batch_op:
        batch_op.create_index("ix_temp_registrations_email", ["email"], unique=False)
        batch_op.create_index("ix_temp_registrations_expires_at", ["expires_at"], unique=False)


def downgrade():
    op.drop_table("temporary_registrations")
