"""Add WorkflowFolder

Revision ID: 61f47f34caa9
Revises: e7c6481e972b
Create Date: 2025-04-26 22:54:48.937577

"""

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 = "61f47f34caa9"
down_revision: str | None = "e7c6481e972b"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


foreign_key = "fk_workflow_folder_id_workflow_folder"


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "workflow_folder",
        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("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("path", sa.String(), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="workflow_folder_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="workflow_folder_pkey"),
        sa.UniqueConstraint("path", "owner_id", name="uq_workflow_folder_path_owner"),
    )
    op.create_index(
        op.f("ix_workflow_folder_id"), "workflow_folder", ["id"], unique=True
    )
    op.create_index(
        op.f("ix_workflow_folder_path"), "workflow_folder", ["path"], unique=False
    )
    op.add_column("workflow", sa.Column("folder_id", sa.UUID(), nullable=True))
    op.create_foreign_key(
        foreign_key,
        "workflow",
        "workflow_folder",
        ["folder_id"],
        ["id"],
        ondelete="CASCADE",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(foreign_key, "workflow", type_="foreignkey")
    op.drop_column("workflow", "folder_id")
    op.drop_index(op.f("ix_workflow_folder_path"), table_name="workflow_folder")
    op.drop_index(op.f("ix_workflow_folder_id"), table_name="workflow_folder")
    op.drop_table("workflow_folder")
    # ### end Alembic commands ###
