"""persist cleaned question insights

Revision ID: 0020_question_insight_store
Revises: 0019_periodic_reports
Create Date: 2026-07-31 00:00:00.000000
"""

from __future__ import annotations

from alembic import context, op
import sqlalchemy as sa


revision = "0020_question_insight_store"
down_revision = "0019_periodic_reports"
branch_labels = None
depends_on = None


def upgrade() -> None:
    table_name = "sys_question_insight_cleaned_question"
    table_exists = False
    existing_indexes: set[str] = set()
    if not context.is_offline_mode():
        inspector = sa.inspect(op.get_bind())
        table_exists = inspector.has_table(table_name)
        if table_exists:
            existing_indexes = {item["name"] for item in inspector.get_indexes(table_name)}

    if not table_exists:
        op.create_table(
            table_name,
            sa.Column(
                "id",
                sa.BigInteger().with_variant(sa.Integer(), "sqlite"),
                primary_key=True,
                autoincrement=True,
            ),
            sa.Column("message_id", sa.BigInteger(), nullable=False),
            sa.Column("session_id", sa.BigInteger(), nullable=False),
            sa.Column("user_id", sa.BigInteger(), nullable=False),
            sa.Column("part_index", sa.Integer(), nullable=False),
            sa.Column("cleaner_version", sa.String(length=20), nullable=False),
            sa.Column("source_hash", sa.String(length=64), nullable=False),
            sa.Column("cleaned_text", sa.Text(), nullable=False),
            sa.Column("normalized_text", sa.Text(), nullable=False),
            sa.Column("category", sa.String(length=50), nullable=False, server_default="other"),
            sa.Column("tokens_json", sa.Text(), nullable=False),
            sa.Column("accepted", sa.Integer(), nullable=False, server_default="1"),
            sa.Column("filtered_reason", sa.String(length=100), nullable=True),
            sa.Column("source_created_at", sa.DateTime(), nullable=False),
            sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
            sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
            sa.ForeignKeyConstraint(
                ["message_id"],
                ["sys_chat_message.id"],
                ondelete="CASCADE",
            ),
            sa.UniqueConstraint(
                "message_id",
                "cleaner_version",
                "part_index",
                name="uq_question_insight_message_version_part",
            ),
        )

    indexes = {
        "ix_sys_question_insight_cleaned_question_session_id": ["session_id"],
        "ix_sys_question_insight_cleaned_question_user_id": ["user_id"],
        "ix_question_insight_version_accepted_created": ["cleaner_version", "accepted", "source_created_at"],
        "ix_question_insight_category_created": ["category", "source_created_at"],
        "ix_question_insight_session_created": ["session_id", "source_created_at"],
    }
    for index_name, columns in indexes.items():
        if index_name not in existing_indexes:
            op.create_index(index_name, table_name, columns)


def downgrade() -> None:
    op.drop_index(
        "ix_question_insight_session_created",
        table_name="sys_question_insight_cleaned_question",
    )
    op.drop_index(
        "ix_question_insight_category_created",
        table_name="sys_question_insight_cleaned_question",
    )
    op.drop_index(
        "ix_question_insight_version_accepted_created",
        table_name="sys_question_insight_cleaned_question",
    )
    op.drop_index(
        "ix_sys_question_insight_cleaned_question_user_id",
        table_name="sys_question_insight_cleaned_question",
    )
    op.drop_index(
        "ix_sys_question_insight_cleaned_question_session_id",
        table_name="sys_question_insight_cleaned_question",
    )
    op.drop_table("sys_question_insight_cleaned_question")
