"""face_embeddings vector 128 -> 512

Revision ID: 0006_face_512d
Revises: 0005_face_embeddings
Create Date: 2026-04-10

Resize face_embeddings.vector from Vector(128) to Vector(512) for
ArcFace embeddings (InsightFace). Drops existing data and HNSW index,
recreates both.
"""
from typing import Sequence, Union

from alembic import op

revision: str = "0006_face_512d"
down_revision: Union[str, None] = "0005_face_embeddings"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Drop index, truncate (old 128-d vectors are incompatible), resize
    op.execute("DROP INDEX IF EXISTS ix_face_embeddings_vector_hnsw")
    op.execute("DELETE FROM face_embeddings")
    op.execute("ALTER TABLE face_embeddings ALTER COLUMN vector TYPE vector(512)")
    op.execute("""
        CREATE INDEX IF NOT EXISTS ix_face_embeddings_vector_hnsw
        ON face_embeddings USING hnsw (vector vector_cosine_ops)
    """)


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS ix_face_embeddings_vector_hnsw")
    op.execute("DELETE FROM face_embeddings")
    op.execute("ALTER TABLE face_embeddings ALTER COLUMN vector TYPE vector(128)")
    op.execute("""
        CREATE INDEX IF NOT EXISTS ix_face_embeddings_vector_hnsw
        ON face_embeddings USING hnsw (vector vector_cosine_ops)
    """)
