"""add verified flags partial unique indexes and otp channel

Revision ID: ed1ee23ab5ed
Revises: 361adf5ed93f
Create Date: 2026-07-20 15:12:10.948739

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "ed1ee23ab5ed"
down_revision: Union[str, None] = "361adf5ed93f"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # NOTE: autogenerate also proposed op.drop_table('spatial_ref_sys') (a
    # PostGIS system table — dropping it would break PostGIS entirely) and
    # dropping refresh_tokens_token_hash_key (pre-existing unique-index-vs-
    # named-constraint drift unrelated to this migration — same drift
    # called out in 42828b3f2eea). Both removed by hand. users_phone_key
    # is DIFFERENT from that recurring drift this time — it's genuinely
    # dropped on purpose here, since phone uniqueness moves to the new
    # partial index below. server_default='true'/'sms' backfill existing
    # rows as already-verified (they went through the old, always-strict
    # signup/invite flow) and already-SMS respectively, then both are
    # dropped so only the Python-side defaults apply going forward.
    op.add_column(
        "otp_verifications",
        sa.Column(
            "channel", sa.String(length=10), nullable=False, server_default="sms"
        ),
    )
    op.alter_column("otp_verifications", "channel", server_default=None)
    op.add_column(
        "users",
        sa.Column(
            "is_phone_verified", sa.Boolean(), nullable=False, server_default=sa.true()
        ),
    )
    op.add_column(
        "users",
        sa.Column(
            "is_email_verified", sa.Boolean(), nullable=False, server_default=sa.true()
        ),
    )
    op.alter_column("users", "is_phone_verified", server_default=None)
    op.alter_column("users", "is_email_verified", server_default=None)
    op.drop_constraint("users_phone_key", "users", type_="unique")
    op.drop_index("ix_users_email", table_name="users")
    op.create_index(op.f("ix_users_email"), "users", ["email"], unique=False)
    op.drop_index("ix_users_phone", table_name="users")
    op.create_index(op.f("ix_users_phone"), "users", ["phone"], unique=False)
    op.create_index(
        "ux_users_email_verified",
        "users",
        ["email"],
        unique=True,
        postgresql_where=sa.text("is_email_verified = true"),
        sqlite_where=sa.text("is_email_verified = 1"),
    )
    op.create_index(
        "ux_users_phone_verified",
        "users",
        ["phone"],
        unique=True,
        postgresql_where=sa.text("is_phone_verified = true"),
        sqlite_where=sa.text("is_phone_verified = 1"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        "ux_users_phone_verified",
        table_name="users",
        postgresql_where=sa.text("is_phone_verified = true"),
        sqlite_where=sa.text("is_phone_verified = 1"),
    )
    op.drop_index(
        "ux_users_email_verified",
        table_name="users",
        postgresql_where=sa.text("is_email_verified = true"),
        sqlite_where=sa.text("is_email_verified = 1"),
    )
    op.drop_index(op.f("ix_users_phone"), table_name="users")
    op.create_index("ix_users_phone", "users", ["phone"], unique=True)
    op.drop_index(op.f("ix_users_email"), table_name="users")
    op.create_index("ix_users_email", "users", ["email"], unique=True)
    op.create_unique_constraint(
        "users_phone_key", "users", ["phone"], postgresql_nulls_not_distinct=False
    )
    op.drop_column("users", "is_email_verified")
    op.drop_column("users", "is_phone_verified")
    op.drop_column("otp_verifications", "channel")
    # ### end Alembic commands ###
