"""Create approval table

Revision ID: b376e6d16619
Revises: dab623699009
Create Date: 2025-10-17 13:37:04.811962

"""

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 = "b376e6d16619"
down_revision: str | None = "dab623699009"
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("PENDING", "APPROVED", "REJECTED", name="approvalstatus").create(
        op.get_bind()
    )
    op.create_table(
        "approval",
        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("session_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("tool_call_id", sa.String(), nullable=False),
        sa.Column("tool_name", sa.String(), nullable=False),
        sa.Column(
            "tool_call_args", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column(
            "status",
            postgresql.ENUM(
                "PENDING",
                "APPROVED",
                "REJECTED",
                name="approvalstatus",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("reason", sa.String(), nullable=True),
        sa.Column("decision", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("approved_by", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("approved_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["approved_by"],
            ["user.id"],
            name="approval_approved_by_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="approval_pkey"),
        sa.UniqueConstraint(
            "owner_id",
            "session_id",
            "tool_call_id",
            name="uq_approval_owner_session_tool",
        ),
    )
    op.create_index(op.f("ix_approval_id"), "approval", ["id"], unique=True)
    op.create_index(
        op.f("ix_approval_session_id"), "approval", ["session_id"], unique=False
    )
    op.create_index(op.f("ix_approval_status"), "approval", ["status"], unique=False)
    op.create_index(
        op.f("ix_approval_tool_call_id"), "approval", ["tool_call_id"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_approval_tool_call_id"), table_name="approval")
    op.drop_index(op.f("ix_approval_status"), table_name="approval")
    op.drop_index(op.f("ix_approval_session_id"), table_name="approval")
    op.drop_index(op.f("ix_approval_id"), table_name="approval")
    op.drop_table("approval")
    sa.Enum("PENDING", "APPROVED", "REJECTED", name="approvalstatus").drop(
        op.get_bind()
    )
    # ### end Alembic commands ###
