"""add_parent_session_id_for_session_forking

Revision ID: bf73d15eca35
Revises: bc5d8f3a2e1d
Create Date: 2026-01-19 02:32:25.433976

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "bf73d15eca35"
down_revision: str | None = "bc5d8f3a2e1d"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "agent_session", sa.Column("parent_session_id", sa.UUID(), nullable=True)
    )
    op.create_index(
        op.f("ix_agent_session_parent_session_id"),
        "agent_session",
        ["parent_session_id"],
        unique=False,
    )
    op.create_foreign_key(
        op.f("fk_agent_session_parent_session_id_agent_session"),
        "agent_session",
        "agent_session",
        ["parent_session_id"],
        ["id"],
        ondelete="SET NULL",
        use_alter=True,
    )
    op.create_index(
        op.f("ix_organization_settings_key"),
        "organization_settings",
        ["key"],
        unique=False,
    )
    op.drop_index(
        "ix_workspace_organization_id", table_name="workspace", if_exists=True
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(
        "ix_workspace_organization_id",
        "workspace",
        ["organization_id"],
        unique=False,
        if_not_exists=True,
    )
    op.drop_index(
        op.f("ix_organization_settings_key"), table_name="organization_settings"
    )
    op.drop_constraint(
        op.f("fk_agent_session_parent_session_id_agent_session"),
        "agent_session",
        type_="foreignkey",
    )
    op.drop_index(
        op.f("ix_agent_session_parent_session_id"), table_name="agent_session"
    )
    op.drop_column("agent_session", "parent_session_id")
    # ### end Alembic commands ###
