"""add_folders_table

Revision ID: 541870962332
Revises: d135e472622c
Create Date: 2025-10-30 14:14:37.593691+00:00

"""

from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "541870962332"
down_revision: Union[str, None] = "d135e472622c"
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! ###
    # Step 1: Create folders table
    op.create_table(
        "folders",
        sa.Column("folder_id", sa.String(), nullable=False),
        sa.Column("organization_id", sa.String(), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("modified_at", sa.DateTime(), nullable=False),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("folder_id"),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.organization_id"],
            ondelete="CASCADE",
        ),
    )

    # Create indexes on folders table
    op.create_index(
        "folder_organization_id_idx",
        "folders",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "folder_organization_title_idx",
        "folders",
        ["organization_id", "title"],
        unique=False,
    )

    # Step 2: Add folder_id to workflows table
    op.add_column("workflows", sa.Column("folder_id", sa.String(), nullable=True))

    # Create index on workflows.folder_id
    op.create_index(
        "workflow_folder_id_idx",
        "workflows",
        ["folder_id"],
        unique=False,
    )

    # Create foreign key constraint
    op.create_foreign_key(
        "fk_workflows_folder_id",
        "workflows",
        "folders",
        ["folder_id"],
        ["folder_id"],
        ondelete="SET NULL",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Step 1: Remove folder_id from workflows table (must be done before dropping folders table)
    op.drop_constraint("fk_workflows_folder_id", "workflows", type_="foreignkey")
    op.drop_index("workflow_folder_id_idx", table_name="workflows")
    op.drop_column("workflows", "folder_id")

    # Step 2: Drop folders table
    op.drop_index("folder_organization_title_idx", table_name="folders")
    op.drop_index("folder_organization_id_idx", table_name="folders")
    op.drop_table("folders")
    # ### end Alembic commands ###
