"""character prototype store (#1317) — precomputed, incremental CCIP references

New tables character_prototype + ccip_prototype_state, plus MLSettings columns
ccip_ref_signature (cheap global change gate) + ccip_prototype_cap (per-character
reference cap). The reference set the CCIP matcher uses becomes a precomputed
artifact refreshed incrementally off the request path. See milestone 138 /
backend.app.services.ml.character_prototypes.

Revision ID: 0079
Revises: 0078
Create Date: 2026-07-06
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from pgvector.sqlalchemy import Vector

revision: str = "0079"
down_revision: Union[str, None] = "0078"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

# Matches models.image_region.CCIP_DIM (the CCIP figure-embedding width).
_CCIP_DIM = 768


def upgrade() -> None:
    op.create_table(
        "character_prototype",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "tag_id", sa.Integer(),
            sa.ForeignKey("tag.id", ondelete="CASCADE"), nullable=False,
        ),
        sa.Column("ccip_embedding", Vector(_CCIP_DIM), nullable=False),
        sa.Column(
            "region_id", sa.Integer(),
            sa.ForeignKey("image_region.id", ondelete="SET NULL"), nullable=True,
        ),
    )
    op.create_index(
        "ix_character_prototype_tag_id", "character_prototype", ["tag_id"]
    )
    op.create_table(
        "ccip_prototype_state",
        sa.Column(
            "tag_id", sa.Integer(),
            sa.ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True,
        ),
        sa.Column("fingerprint", sa.String(64), nullable=False),
        sa.Column(
            "updated_at", sa.DateTime(timezone=True), nullable=False,
            server_default=sa.func.now(),
        ),
    )
    op.add_column(
        "ml_settings",
        sa.Column("ccip_ref_signature", sa.String(128), nullable=True),
    )
    op.add_column(
        "ml_settings",
        sa.Column(
            "ccip_prototype_cap", sa.Integer(), nullable=False,
            server_default=sa.text("64"),
        ),
    )


def downgrade() -> None:
    op.drop_column("ml_settings", "ccip_prototype_cap")
    op.drop_column("ml_settings", "ccip_ref_signature")
    op.drop_table("ccip_prototype_state")
    op.drop_index(
        "ix_character_prototype_tag_id", table_name="character_prototype"
    )
    op.drop_table("character_prototype")
