"""workflows and run state

Revision ID: c7a5347c0896
Revises: b74db0e1c0bf
Create Date: 2026-07-01 15:49:32.622833
"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "c7a5347c0896"
down_revision: str | None = "b74db0e1c0bf"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "workflows",
        sa.Column("workspace_id", sa.Uuid(), nullable=False),
        sa.Column("created_by", sa.Uuid(), nullable=True),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("description", sa.String(length=1024), nullable=False),
        sa.Column("definition_json", sa.JSON(), nullable=False),
        sa.Column("trigger_type", sa.String(length=20), nullable=False),
        sa.Column("trigger_config", sa.JSON(), nullable=False),
        sa.Column("webhook_token", sa.String(length=64), nullable=True),
        sa.Column("webhook_secret", sa.String(length=128), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("last_triggered_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(["created_by"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["workspace_id"], ["workspaces.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_workflows_webhook_token"), "workflows", ["webhook_token"], unique=True)
    op.create_index(op.f("ix_workflows_workspace_id"), "workflows", ["workspace_id"], unique=False)
    # Batch mode so the new column + FK also apply on SQLite (copy-and-move).
    with op.batch_alter_table("runs", schema=None) as batch_op:
        batch_op.add_column(sa.Column("workflow_id", sa.Uuid(), nullable=True))
        batch_op.add_column(sa.Column("state_json", sa.JSON(), nullable=True))
        batch_op.create_index(batch_op.f("ix_runs_workflow_id"), ["workflow_id"], unique=False)
        batch_op.create_foreign_key(
            "fk_runs_workflow_id_workflows",
            "workflows",
            ["workflow_id"],
            ["id"],
            ondelete="SET NULL",
        )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("runs", schema=None) as batch_op:
        batch_op.drop_constraint("fk_runs_workflow_id_workflows", type_="foreignkey")
        batch_op.drop_index(batch_op.f("ix_runs_workflow_id"))
        batch_op.drop_column("state_json")
        batch_op.drop_column("workflow_id")
    op.drop_index(op.f("ix_workflows_workspace_id"), table_name="workflows")
    op.drop_index(op.f("ix_workflows_webhook_token"), table_name="workflows")
    op.drop_table("workflows")
    # ### end Alembic commands ###
