"""convert workstream status enum to string

Revision ID: r5m6n7o8p9q0
Revises: q4l5m6n7o8p9
Create Date: 2026-05-01

"""
from alembic import op
import sqlalchemy as sa

revision = "r5m6n7o8p9q0"
down_revision = "q4l5m6n7o8p9"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.alter_column("workstreams", "status", server_default=None)
    op.alter_column(
        "workstreams",
        "status",
        existing_type=sa.Enum(
            "active", "blocked", "completed", "archived", name="workstreamstatus"
        ),
        type_=sa.String(length=20),
        existing_nullable=False,
        postgresql_using="status::text",
        server_default="active",
    )
    op.execute("DROP TYPE IF EXISTS workstreamstatus")


def downgrade() -> None:
    op.execute("UPDATE workstreams SET status = 'active' WHERE status = 'todo'")
    op.alter_column("workstreams", "status", server_default=None)
    workstream_status = sa.Enum(
        "active", "blocked", "completed", "archived", name="workstreamstatus"
    )
    workstream_status.create(op.get_bind(), checkfirst=True)
    op.alter_column(
        "workstreams",
        "status",
        existing_type=sa.String(length=20),
        type_=workstream_status,
        existing_nullable=False,
        postgresql_using="status::workstreamstatus",
        server_default="active",
    )
