"""create_notebook_table

Revision ID: f617c5594fbf
Revises: 88a3cbf21434
Create Date: 2025-11-21 16:05:38.673677

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "f617c5594fbf"
down_revision = "88a3cbf21434"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "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(
            "prompt_configs",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column(
            "prompt_variable_mapping",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column("dataset_id", sa.String(), 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_notebooks_created_at"),
        "notebooks",
        ["created_at"],
        unique=False,
    )
    op.create_index(op.f("ix_notebooks_id"), "notebooks", ["id"], unique=False)
    op.create_index(
        op.f("ix_notebooks_task_id"),
        "notebooks",
        ["task_id"],
        unique=False,
    )
    op.add_column(
        "prompt_experiments",
        sa.Column("notebook_id", sa.String(), nullable=True),
    )
    op.create_index(
        op.f("ix_prompt_experiments_notebook_id"),
        "prompt_experiments",
        ["notebook_id"],
        unique=False,
    )
    op.create_foreign_key(
        None,
        "prompt_experiments",
        "notebooks",
        ["notebook_id"],
        ["id"],
        ondelete="SET NULL",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "prompt_experiments", type_="foreignkey")
    op.drop_index(
        op.f("ix_prompt_experiments_notebook_id"),
        table_name="prompt_experiments",
    )
    op.drop_column("prompt_experiments", "notebook_id")
    op.drop_index(op.f("ix_notebooks_task_id"), table_name="notebooks")
    op.drop_index(op.f("ix_notebooks_id"), table_name="notebooks")
    op.drop_index(op.f("ix_notebooks_created_at"), table_name="notebooks")
    op.drop_table("notebooks")
    # ### end Alembic commands ###
