"""create agentic notebooks

Revision ID: a1b2c3d4e5f6
Revises: 06b0610b582c
Create Date: 2026-01-06 10:10:00.000000

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "a1b2c3d4e5f6"
down_revision = "06b0610b582c"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "agentic_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(
            "http_template",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column(
            "template_variable_mapping",
            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_agentic_notebooks_created_at"),
        "agentic_notebooks",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_agentic_notebooks_task_id"),
        "agentic_notebooks",
        ["task_id"],
        unique=False,
    )
    # Add notebook_id column to agentic_experiments
    op.add_column(
        "agentic_experiments",
        sa.Column("notebook_id", sa.String(), nullable=True),
    )
    op.create_index(
        op.f("ix_agentic_experiments_notebook_id"),
        "agentic_experiments",
        ["notebook_id"],
        unique=False,
    )
    op.create_foreign_key(
        "agentic_experiments_notebooks_fkey",
        "agentic_experiments",
        "agentic_notebooks",
        ["notebook_id"],
        ["id"],
        ondelete="SET NULL",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        "agentic_experiments_notebooks_fkey",
        "agentic_experiments",
        type_="foreignkey",
    )
    op.drop_index(
        op.f("ix_agentic_experiments_notebook_id"),
        table_name="agentic_experiments",
    )
    op.drop_column("agentic_experiments", "notebook_id")
    op.drop_index(
        op.f("ix_agentic_notebooks_task_id"),
        table_name="agentic_notebooks",
    )
    op.drop_index(
        op.f("ix_agentic_notebooks_created_at"),
        table_name="agentic_notebooks",
    )
    op.drop_table("agentic_notebooks")
    # ### end Alembic commands ###
