"""add workplan execution queue and launch requests

Revision ID: x1s2t3u4v5w6
Revises: w0r1s2t3u4v5
Create Date: 2026-05-23

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB, UUID

revision = "x1s2t3u4v5w6"
down_revision = "w0r1s2t3u4v5"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "workstreams",
        sa.Column("execution_state", sa.String(length=20), nullable=False, server_default="manual"),
    )
    op.add_column(
        "workstreams",
        sa.Column("launch_mode", sa.String(length=20), nullable=False, server_default="manual"),
    )
    op.add_column(
        "workstreams",
        sa.Column("concurrency_mode", sa.String(length=20), nullable=False, server_default="sequential"),
    )
    op.add_column("workstreams", sa.Column("queue_rank", sa.Integer(), nullable=True))
    op.add_column("workstreams", sa.Column("execution_group", sa.String(length=100), nullable=True))
    op.add_column("workstreams", sa.Column("scheduled_for", sa.DateTime(timezone=True), nullable=True))
    op.create_index("ix_workstreams_execution_state", "workstreams", ["execution_state"])
    op.create_index("ix_workstreams_launch_mode", "workstreams", ["launch_mode"])
    op.create_index("ix_workstreams_concurrency_mode", "workstreams", ["concurrency_mode"])
    op.create_index("ix_workstreams_queue_rank", "workstreams", ["queue_rank"])
    op.create_index("ix_workstreams_execution_group", "workstreams", ["execution_group"])
    op.create_index("ix_workstreams_scheduled_for", "workstreams", ["scheduled_for"])

    op.create_table(
        "workplan_launch_requests",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, nullable=False),
        sa.Column("workstream_id", UUID(as_uuid=True), nullable=False),
        sa.Column("requested_by", sa.String(length=100), nullable=False),
        sa.Column("requested_actor", sa.String(length=100), nullable=True),
        sa.Column("launch_mode", sa.String(length=20), nullable=False),
        sa.Column("concurrency_mode", sa.String(length=20), nullable=False),
        sa.Column("priority", sa.String(length=20), nullable=True),
        sa.Column("repo_id", UUID(as_uuid=True), nullable=True),
        sa.Column("branch_preference", sa.Text(), nullable=True),
        sa.Column("immediate_pickup", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="requested"),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("request_metadata", JSONB(), nullable=False, server_default="{}"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.ForeignKeyConstraint(["repo_id"], ["managed_repos.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["workstream_id"], ["workstreams.id"], ondelete="CASCADE"),
    )
    op.create_index("ix_workplan_launch_requests_workstream_id", "workplan_launch_requests", ["workstream_id"])
    op.create_index("ix_workplan_launch_requests_launch_mode", "workplan_launch_requests", ["launch_mode"])
    op.create_index("ix_workplan_launch_requests_concurrency_mode", "workplan_launch_requests", ["concurrency_mode"])
    op.create_index("ix_workplan_launch_requests_priority", "workplan_launch_requests", ["priority"])
    op.create_index("ix_workplan_launch_requests_repo_id", "workplan_launch_requests", ["repo_id"])
    op.create_index("ix_workplan_launch_requests_status", "workplan_launch_requests", ["status"])


def downgrade() -> None:
    op.drop_index("ix_workplan_launch_requests_status", table_name="workplan_launch_requests")
    op.drop_index("ix_workplan_launch_requests_repo_id", table_name="workplan_launch_requests")
    op.drop_index("ix_workplan_launch_requests_priority", table_name="workplan_launch_requests")
    op.drop_index("ix_workplan_launch_requests_concurrency_mode", table_name="workplan_launch_requests")
    op.drop_index("ix_workplan_launch_requests_launch_mode", table_name="workplan_launch_requests")
    op.drop_index("ix_workplan_launch_requests_workstream_id", table_name="workplan_launch_requests")
    op.drop_table("workplan_launch_requests")

    op.drop_index("ix_workstreams_scheduled_for", table_name="workstreams")
    op.drop_index("ix_workstreams_execution_group", table_name="workstreams")
    op.drop_index("ix_workstreams_queue_rank", table_name="workstreams")
    op.drop_index("ix_workstreams_concurrency_mode", table_name="workstreams")
    op.drop_index("ix_workstreams_launch_mode", table_name="workstreams")
    op.drop_index("ix_workstreams_execution_state", table_name="workstreams")
    op.drop_column("workstreams", "scheduled_for")
    op.drop_column("workstreams", "execution_group")
    op.drop_column("workstreams", "queue_rank")
    op.drop_column("workstreams", "concurrency_mode")
    op.drop_column("workstreams", "launch_mode")
    op.drop_column("workstreams", "execution_state")
