"""Run wait conditions [24771f985eac].

Revision ID: 24771f985eac
Revises: 0.94.0
Create Date: 2026-03-13 15:16:21.636758

"""

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision = "24771f985eac"
down_revision = "0.94.0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Upgrade database schema and/or data, creating a new revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "run_wait_condition",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("run_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("project_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=True),
        sa.Column(
            "resolved_by_user_id", sqlmodel.sql.sqltypes.GUID(), nullable=True
        ),
        sa.Column("type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "status", sqlmodel.sql.sqltypes.AutoString(), nullable=False
        ),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "question", sqlmodel.sql.sqltypes.AutoString(), nullable=True
        ),
        sa.Column("data_schema_json", sa.TEXT(), nullable=True),
        sa.Column(
            "resolution", sqlmodel.sql.sqltypes.AutoString(), nullable=True
        ),
        sa.Column("result_json", sa.TEXT(), nullable=True),
        sa.Column("last_polled_at", sa.DateTime(), nullable=True),
        sa.Column(
            "poller_instance_id",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column("poller_lease_expires_at", sa.DateTime(), nullable=True),
        sa.Column("resolved_at", sa.DateTime(), nullable=True),
        sa.CheckConstraint(
            "(status != 'resolved') OR (resolution IS NOT NULL)",
            name="ck_run_wait_condition_resolved_requires_resolution",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_run_wait_condition_project_id_project",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="fk_run_wait_condition_user_id_user",
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["resolved_by_user_id"],
            ["user.id"],
            name="fk_run_wait_condition_resolved_by_user_id_user",
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["run_id"],
            ["pipeline_run.id"],
            name="fk_run_wait_condition_run_id_pipeline_run",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "run_id", "name", name="unique_wait_condition_name_per_run"
        ),
    )
    with op.batch_alter_table("run_wait_condition", schema=None) as batch_op:
        batch_op.create_index(
            "ix_run_wait_condition_project_id_created",
            ["project_id", "created"],
            unique=False,
        )
        batch_op.create_index(
            "ix_run_wait_condition_run_id_status",
            ["run_id", "status"],
            unique=False,
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade database schema and/or data back to the previous revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("run_wait_condition", schema=None) as batch_op:
        batch_op.drop_index("ix_run_wait_condition_run_id_status")
        batch_op.drop_index("ix_run_wait_condition_project_id_created")

    op.drop_table("run_wait_condition")
    # ### end Alembic commands ###
