"""rename_summary_to_instructions_add_version_drop_content

Revision ID: 3c55aa60ca2c
Revises: 2b82bb8c7c42
Create Date: 2025-09-21 15:00:03.008286

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "3c55aa60ca2c"
down_revision: str | None = "2b82bb8c7c42"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Create the RunbookCaseLink table
    op.create_table(
        "runbookcaselink",
        sa.Column("runbook_id", sa.UUID(), nullable=False),
        sa.Column("case_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="runbookcaselink_case_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["runbook_id"],
            ["runbook.id"],
            ondelete="CASCADE",
            name="runbookcaselink_runbook_id_fkey",
        ),
        sa.PrimaryKeyConstraint("runbook_id", "case_id", name="runbookcaselink_pkey"),
    )

    # Add version column with default value
    op.add_column(
        "runbook",
        sa.Column("version", sa.Integer(), nullable=False, server_default="1"),
    )

    # First update any null summary values with empty string
    op.execute("UPDATE runbook SET summary = '' WHERE summary IS NULL")

    # Rename summary to instructions (preserves data)
    op.alter_column(
        "runbook",
        "summary",
        new_column_name="instructions",
        existing_type=sa.VARCHAR(),
        nullable=False,
    )

    # Ensure the alias constraint matches the current naming convention
    op.drop_constraint("uq_runbook_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_runbook_alias_owner_id",
        "runbook",
        ["alias", "owner_id"],
        postgresql_nulls_not_distinct=False,
    )

    # Drop unused columns
    op.drop_column("runbook", "meta")
    op.drop_column("runbook", "content")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Re-add the content and meta columns
    op.add_column(
        "runbook",
        sa.Column(
            "content",
            sa.VARCHAR(),
            autoincrement=False,
            nullable=False,
            server_default="",
        ),
    )
    op.add_column(
        "runbook",
        sa.Column(
            "meta",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
    )

    # Rename instructions back to summary
    op.alter_column(
        "runbook",
        "instructions",
        new_column_name="summary",
        existing_type=sa.VARCHAR(),
        nullable=False,
    )

    # Restore the constraint using the same naming/options as pre-upgrade state
    op.drop_constraint("uq_runbook_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_runbook_alias_owner_id",
        "runbook",
        ["alias", "owner_id"],
        postgresql_nulls_not_distinct=False,
    )

    # Drop the version column
    op.drop_column("runbook", "version")

    # Drop the RunbookCaseLink table
    op.drop_table("runbookcaselink")
    # ### end Alembic commands ###
