"""adapt task status enum to InfoTechCanon task lifecycle

Revision ID: a4v5w6x7y8z9
Revises: z3u4v5w6x7y8
Create Date: 2026-05-25

"""
from alembic import op

revision = "a4v5w6x7y8z9"
down_revision = "z3u4v5w6x7y8"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("ALTER TABLE tasks ALTER COLUMN status DROP DEFAULT")
    op.execute("ALTER TYPE taskstatus RENAME TO taskstatus_old")
    op.execute("CREATE TYPE taskstatus AS ENUM ('wait', 'todo', 'progress', 'done', 'cancel')")
    op.execute(
        """
        ALTER TABLE tasks
        ALTER COLUMN status TYPE taskstatus
        USING (
            CASE status::text
                WHEN 'blocked' THEN 'wait'
                WHEN 'in_progress' THEN 'progress'
                WHEN 'cancelled' THEN 'cancel'
                ELSE status::text
            END
        )::taskstatus
        """
    )
    op.execute("ALTER TABLE tasks ALTER COLUMN status SET DEFAULT 'todo'")
    op.execute("DROP TYPE taskstatus_old")


def downgrade() -> None:
    op.execute("ALTER TABLE tasks ALTER COLUMN status DROP DEFAULT")
    op.execute("ALTER TYPE taskstatus RENAME TO taskstatus_new")
    op.execute("CREATE TYPE taskstatus AS ENUM ('todo', 'in_progress', 'blocked', 'done', 'cancelled')")
    op.execute(
        """
        ALTER TABLE tasks
        ALTER COLUMN status TYPE taskstatus
        USING (
            CASE status::text
                WHEN 'wait' THEN 'blocked'
                WHEN 'progress' THEN 'in_progress'
                WHEN 'cancel' THEN 'cancelled'
                ELSE status::text
            END
        )::taskstatus
        """
    )
    op.execute("ALTER TABLE tasks ALTER COLUMN status SET DEFAULT 'todo'")
    op.execute("DROP TYPE taskstatus_new")
