"""Add rss_item_embeddings table for semantic news search."""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB

revision = "0035"
down_revision = "0034"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "rss_item_embeddings",
        sa.Column("rss_item_id", sa.Integer(), sa.ForeignKey("rss_items.id", ondelete="CASCADE"), primary_key=True),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("embedding", JSONB(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )
    op.create_index("ix_rss_item_embeddings_user_id", "rss_item_embeddings", ["user_id"])
    op.create_index("ix_rss_item_embeddings_rss_item_id", "rss_item_embeddings", ["rss_item_id"])


def downgrade() -> None:
    op.drop_index("ix_rss_item_embeddings_rss_item_id", table_name="rss_item_embeddings")
    op.drop_index("ix_rss_item_embeddings_user_id", table_name="rss_item_embeddings")
    op.drop_table("rss_item_embeddings")
