"""add agent trigger

Revision ID: c7c56dd36b21
Revises: 49a8fe6841e6
Create Date: 2025-12-21 08:21:06.692772

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "c7c56dd36b21"
down_revision: Union[str, None] = "49a8fe6841e6"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    sa.Enum("active", "archived", name="trigger_state").create(op.get_bind())
    op.create_table(
        "agent_triggers",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("trigger_path", sa.String(length=36), nullable=False),
        sa.Column("workflow_id", sa.Integer(), nullable=False),
        sa.Column("organization_id", sa.Integer(), nullable=False),
        sa.Column(
            "state",
            postgresql.ENUM(
                "active", "archived", name="trigger_state", create_type=False
            ),
            server_default=sa.text("'active'::trigger_state"),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"], ["organizations.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(["workflow_id"], ["workflows.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "ix_agent_triggers_state", "agent_triggers", ["state"], unique=False
    )
    op.create_index(
        op.f("ix_agent_triggers_trigger_path"),
        "agent_triggers",
        ["trigger_path"],
        unique=True,
    )
    op.create_index(
        "ix_agent_triggers_workflow_id", "agent_triggers", ["workflow_id"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("ix_agent_triggers_workflow_id", table_name="agent_triggers")
    op.drop_index(op.f("ix_agent_triggers_trigger_path"), table_name="agent_triggers")
    op.drop_index("ix_agent_triggers_state", table_name="agent_triggers")
    op.drop_table("agent_triggers")
    sa.Enum("active", "archived", name="trigger_state").drop(op.get_bind())
    # ### end Alembic commands ###
