"""Add is_system and type to registry repos

Revision ID: 14b4de0bbc0e
Revises: ef08aba23d77
Create Date: 2025-05-17 01:12:21.305684

"""

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 = "14b4de0bbc0e"
down_revision: str | None = "ef08aba23d77"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    sa.Enum(
        "IDLE", "PENDING", "ERROR", "TIMED_OUT", "COMPLETED", name="interactionstatus"
    ).create(op.get_bind())
    sa.Enum("APPROVAL", "RESPONSE", name="interactiontype").create(op.get_bind())
    op.create_table(
        "interaction",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("wf_exec_id", sa.String(), nullable=False),
        sa.Column("action_ref", sa.String(), nullable=False),
        sa.Column("action_type", sa.String(), nullable=False),
        sa.Column(
            "type",
            postgresql.ENUM(
                "APPROVAL", "RESPONSE", name="interactiontype", create_type=False
            ),
            nullable=False,
        ),
        sa.Column(
            "status",
            postgresql.ENUM(
                "IDLE",
                "PENDING",
                "ERROR",
                "TIMED_OUT",
                "COMPLETED",
                name="interactionstatus",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column(
            "request_payload", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column(
            "response_payload", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("expires_at", sa.DateTime(), nullable=True),
        sa.Column("actor", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="interaction_pkey"),
    )
    op.create_index(op.f("ix_interaction_id"), "interaction", ["id"], unique=True)
    op.create_index(
        op.f("ix_interaction_status"), "interaction", ["status"], unique=False
    )
    op.create_index(
        op.f("ix_interaction_wf_exec_id"), "interaction", ["wf_exec_id"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_interaction_wf_exec_id"), table_name="interaction")
    op.drop_index(op.f("ix_interaction_status"), table_name="interaction")
    op.drop_index(op.f("ix_interaction_id"), table_name="interaction")
    op.drop_table("interaction")
    sa.Enum("APPROVAL", "RESPONSE", name="interactiontype").drop(op.get_bind())
    sa.Enum(
        "IDLE", "PENDING", "ERROR", "TIMED_OUT", "COMPLETED", name="interactionstatus"
    ).drop(op.get_bind())
    # ### end Alembic commands ###
