"""create rag notebooks

Revision ID: 8aba08c2934f
Revises: 522b83a56135
Create Date: 2025-12-18 13:40:31.375870

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "8aba08c2934f"
down_revision = "522b83a56135"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "rag_notebooks",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("task_id", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("rag_configs", postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column("dataset_id", sa.UUID(), nullable=True),
        sa.Column("dataset_version", sa.Integer(), nullable=True),
        sa.Column(
            "dataset_row_filter",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column(
            "eval_configs",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.ForeignKeyConstraint(["task_id"], ["tasks.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_rag_notebooks_created_at"),
        "rag_notebooks",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_rag_notebooks_task_id"),
        "rag_notebooks",
        ["task_id"],
        unique=False,
    )
    op.drop_constraint(
        op.f("rag_experiments_notebook_id_fkey"),
        "rag_experiments",
        type_="foreignkey",
    )

    # the notebook_id column shouldn't have existed in rag_experiments before now—it was referencing the wrong
    # table and it should never have been set (we had no rag notebook support at the time). let's just set it to null
    # to be sure this migration can't fail.
    op.execute(
        "UPDATE rag_experiments SET notebook_id = NULL WHERE notebook_id IS NOT NULL",
    )
    op.create_foreign_key(
        "rag_experiments_notebooks_fkey",
        "rag_experiments",
        "rag_notebooks",
        ["notebook_id"],
        ["id"],
        ondelete="SET NULL",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        "rag_experiments_notebooks_fkey",
        "rag_experiments",
        type_="foreignkey",
    )
    op.create_foreign_key(
        op.f("rag_experiments_notebook_id_fkey"),
        "rag_experiments",
        "notebooks",
        ["notebook_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.drop_index(op.f("ix_rag_notebooks_task_id"), table_name="rag_notebooks")
    op.drop_index(op.f("ix_rag_notebooks_created_at"), table_name="rag_notebooks")
    op.drop_table("rag_notebooks")
    # ### end Alembic commands ###
