"""add versioning in workflow definitions

Revision ID: a399b39479fe
Revises: c71db647d354
Create Date: 2026-04-07 14:43:50.042973

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "a399b39479fe"
down_revision: Union[str, None] = "c71db647d354"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_api_keys_key_hash"), table_name="api_keys")
    op.create_index(op.f("ix_api_keys_key_hash"), "api_keys", ["key_hash"], unique=True)
    op.add_column(
        "workflow_definitions",
        sa.Column(
            "status", sa.String(), server_default=sa.text("'published'"), nullable=False
        ),
    )
    op.add_column(
        "workflow_definitions", sa.Column("version_number", sa.Integer(), nullable=True)
    )
    op.add_column(
        "workflow_definitions",
        sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "workflow_definitions",
        sa.Column(
            "workflow_configurations",
            sa.JSON(),
            server_default=sa.text("'{}'::json"),
            nullable=False,
        ),
    )
    op.add_column(
        "workflow_definitions",
        sa.Column(
            "template_context_variables",
            sa.JSON(),
            server_default=sa.text("'{}'::json"),
            nullable=False,
        ),
    )
    op.add_column(
        "workflow_definitions",
        sa.Column(
            "call_disposition_codes",
            sa.JSON(),
            server_default=sa.text("'{}'::json"),
            nullable=False,
        ),
    )
    op.alter_column(
        "workflow_definitions",
        "workflow_hash",
        existing_type=sa.VARCHAR(),
        nullable=True,
    )
    op.drop_index(
        op.f("ix_workflow_hash_workflow_id"), table_name="workflow_definitions"
    )
    op.drop_constraint(
        op.f("uq_workflow_hash_workflow_id"), "workflow_definitions", type_="unique"
    )
    op.create_index(
        "ix_workflow_definitions_workflow_status",
        "workflow_definitions",
        ["workflow_id", "status"],
        unique=False,
    )
    op.add_column(
        "workflows", sa.Column("released_definition_id", sa.Integer(), nullable=True)
    )
    op.create_foreign_key(
        None,
        "workflows",
        "workflow_definitions",
        ["released_definition_id"],
        ["id"],
        use_alter=True,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "workflows", type_="foreignkey")
    op.drop_column("workflows", "released_definition_id")
    op.drop_index(
        "ix_workflow_definitions_workflow_status", table_name="workflow_definitions"
    )
    op.create_unique_constraint(
        op.f("uq_workflow_hash_workflow_id"),
        "workflow_definitions",
        ["workflow_hash", "workflow_id"],
        postgresql_nulls_not_distinct=False,
    )
    op.create_index(
        op.f("ix_workflow_hash_workflow_id"),
        "workflow_definitions",
        ["workflow_hash", "workflow_id"],
        unique=False,
    )
    op.alter_column(
        "workflow_definitions",
        "workflow_hash",
        existing_type=sa.VARCHAR(),
        nullable=False,
    )
    op.drop_column("workflow_definitions", "call_disposition_codes")
    op.drop_column("workflow_definitions", "template_context_variables")
    op.drop_column("workflow_definitions", "workflow_configurations")
    op.drop_column("workflow_definitions", "published_at")
    op.drop_column("workflow_definitions", "version_number")
    op.drop_column("workflow_definitions", "status")
    op.drop_index(op.f("ix_api_keys_key_hash"), table_name="api_keys")
    op.create_index(
        op.f("ix_api_keys_key_hash"), "api_keys", ["key_hash"], unique=False
    )
    # ### end Alembic commands ###
