"""Add email_verifications.purpose (generic verification framework).

Turns the single-purpose email-verification table into the backing store for a
purpose-isolated verification framework. Existing rows are email-verification
codes, so they backfill to that purpose via the server default. One table still
backs every purpose — no second OTP table.

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

"""
from alembic import op
import sqlalchemy as sa


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

_PURPOSE = sa.Enum(
    "email_verification", "password_reset", "email_change",
    "login_verification", "two_factor",
    name="verification_purpose",
)


def upgrade():
    with op.batch_alter_table("email_verifications", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("purpose", _PURPOSE, nullable=False, server_default="email_verification")
        )
        batch_op.create_index(
            "ix_email_verifications_user_purpose", ["user_id", "purpose"], unique=False
        )


def downgrade():
    with op.batch_alter_table("email_verifications", schema=None) as batch_op:
        batch_op.drop_index("ix_email_verifications_user_purpose")
        batch_op.drop_column("purpose")
