"""add auth_login_throttle table for exponential back-off throttling

Revision ID: 20260621_07_auth_login_throttle
Revises: 20260611_06_merge_location_poo_tables
Create Date: 2026-06-21 00:00:00.000000
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "20260621_07_auth_login_throttle"
down_revision: Union[str, None] = "20260611_06_merge_location_poo_tables"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "auth_login_throttle",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("key", sa.String(length=255), nullable=False),
        sa.Column("scope", sa.String(length=16), nullable=False),
        sa.Column("failures", sa.Integer(), nullable=False),
        sa.Column("first_failed_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("last_failed_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("next_allowed_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("scope", "key", name="uq_auth_login_throttle_scope_key"),
    )
    op.create_index(
        "ix_auth_login_throttle_scope_key",
        "auth_login_throttle",
        ["scope", "key"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index("ix_auth_login_throttle_scope_key", table_name="auth_login_throttle")
    op.drop_table("auth_login_throttle")
