"""Add metadata for assistant table and cascade del for required foreign keys

Revision ID: aee821a02fc8
Revises: 76dfdbe90d2b
Create Date: 2025-09-13 21:35:35.407522

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "aee821a02fc8"
down_revision = "76dfdbe90d2b"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "assistant",
        sa.Column(
            "metadata",
            postgresql.JSONB(astext_type=sa.Text()),
            server_default=sa.text("'{}'::jsonb"),
            nullable=False,
        ),
    )
    op.drop_constraint(
        op.f("assistant_versions_assistant_id_fkey"),
        "assistant_versions",
        type_="foreignkey",
    )
    op.create_foreign_key(
        None,
        "assistant_versions",
        "assistant",
        ["assistant_id"],
        ["assistant_id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(op.f("runs_assistant_id_fkey"), "runs", type_="foreignkey")
    op.create_foreign_key(
        None,
        "runs",
        "assistant",
        ["assistant_id"],
        ["assistant_id"],
        ondelete="CASCADE",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "runs", type_="foreignkey")
    op.create_foreign_key(
        op.f("runs_assistant_id_fkey"),
        "runs",
        "assistant",
        ["assistant_id"],
        ["assistant_id"],
    )
    op.drop_constraint(None, "assistant_versions", type_="foreignkey")
    op.create_foreign_key(
        op.f("assistant_versions_assistant_id_fkey"),
        "assistant_versions",
        "assistant",
        ["assistant_id"],
        ["assistant_id"],
    )
    op.drop_column("assistant", "metadata")
    # ### end Alembic commands ###
