"""Initial schema migration.

Revision ID: 658dca8f4a11
Revises:
Create Date: 2026-02-09 00:41:55.760624

"""

from __future__ import annotations

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision = "658dca8f4a11"
down_revision = None
branch_labels = None
depends_on = None




def upgrade() -> None:
    """Create initial schema objects."""
    _upgrade_part_1()
    _upgrade_part_2()
    _upgrade_part_3()
    _upgrade_part_4()


def _upgrade_part_1() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "organizations",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name", name="uq_organizations_name"),
    )
    op.create_index(
        op.f("ix_organizations_name"), "organizations", ["name"], unique=False,
    )
    op.create_table(
        "board_groups",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("slug", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_board_groups_organization_id"),
        "board_groups",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_board_groups_slug"), "board_groups", ["slug"], unique=False,
    )
    op.create_table(
        "gateways",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("url", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("token", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column(
            "main_session_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False,
        ),
        sa.Column("workspace_root", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_gateways_organization_id"),
        "gateways",
        ["organization_id"],
        unique=False,
    )
    op.create_table(
        "users",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("clerk_user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("preferred_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("pronouns", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("timezone", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("notes", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("context", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("is_super_admin", sa.Boolean(), nullable=False),
        sa.Column("active_organization_id", sa.Uuid(), nullable=True),
        sa.ForeignKeyConstraint(
            ["active_organization_id"],
            ["organizations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_users_active_organization_id"),
        "users",
        ["active_organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_users_clerk_user_id"), "users", ["clerk_user_id"], unique=True,
    )
    op.create_index(op.f("ix_users_email"), "users", ["email"], unique=False)
    op.create_table(
        "board_group_memory",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_group_id", sa.Uuid(), nullable=False),
        sa.Column("content", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("tags", sa.JSON(), nullable=True),
        sa.Column("is_chat", sa.Boolean(), nullable=False),
        sa.Column("source", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_group_id"],
            ["board_groups.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_board_group_memory_board_group_id"),
        "board_group_memory",
        ["board_group_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_board_group_memory_is_chat"),
        "board_group_memory",
        ["is_chat"],
        unique=False,
    )
    op.create_table(
        "boards",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("slug", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("gateway_id", sa.Uuid(), nullable=True),
        sa.Column("board_group_id", sa.Uuid(), nullable=True),
        sa.Column("board_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("objective", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("success_metrics", sa.JSON(), nullable=True),
        sa.Column("target_date", sa.DateTime(), nullable=True),
        sa.Column("goal_confirmed", sa.Boolean(), nullable=False),
        sa.Column("goal_source", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_group_id"],
            ["board_groups.id"],
        ),
        sa.ForeignKeyConstraint(
            ["gateway_id"],
            ["gateways.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_boards_board_group_id"), "boards", ["board_group_id"], unique=False,
    )
    op.create_index(
        op.f("ix_boards_board_type"), "boards", ["board_type"], unique=False,
    )
    op.create_index(
        op.f("ix_boards_gateway_id"), "boards", ["gateway_id"], unique=False,
    )
    op.create_index(
        op.f("ix_boards_organization_id"), "boards", ["organization_id"], unique=False,
    )
    op.create_index(op.f("ix_boards_slug"), "boards", ["slug"], unique=False)


def _upgrade_part_2() -> None:
    op.create_table(
        "organization_invites",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("invited_email", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("token", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("role", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("all_boards_read", sa.Boolean(), nullable=False),
        sa.Column("all_boards_write", sa.Boolean(), nullable=False),
        sa.Column("created_by_user_id", sa.Uuid(), nullable=True),
        sa.Column("accepted_by_user_id", sa.Uuid(), nullable=True),
        sa.Column("accepted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["accepted_by_user_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["created_by_user_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("token", name="uq_org_invites_token"),
    )
    op.create_index(
        op.f("ix_organization_invites_accepted_by_user_id"),
        "organization_invites",
        ["accepted_by_user_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invites_created_by_user_id"),
        "organization_invites",
        ["created_by_user_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invites_invited_email"),
        "organization_invites",
        ["invited_email"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invites_organization_id"),
        "organization_invites",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invites_role"),
        "organization_invites",
        ["role"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invites_token"),
        "organization_invites",
        ["token"],
        unique=False,
    )
    op.create_table(
        "organization_members",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("user_id", sa.Uuid(), nullable=False),
        sa.Column("role", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("all_boards_read", sa.Boolean(), nullable=False),
        sa.Column("all_boards_write", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_id", "user_id", name="uq_organization_members_org_user",
        ),
    )
    op.create_index(
        op.f("ix_organization_members_organization_id"),
        "organization_members",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_members_role"),
        "organization_members",
        ["role"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_members_user_id"),
        "organization_members",
        ["user_id"],
        unique=False,
    )
    op.create_table(
        "agents",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "openclaw_session_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True,
        ),
        sa.Column(
            "agent_token_hash", sqlmodel.sql.sqltypes.AutoString(), nullable=True,
        ),
        sa.Column("heartbeat_config", sa.JSON(), nullable=True),
        sa.Column("identity_profile", sa.JSON(), nullable=True),
        sa.Column("identity_template", sa.Text(), nullable=True),
        sa.Column("soul_template", sa.Text(), nullable=True),
        sa.Column("provision_requested_at", sa.DateTime(), nullable=True),
        sa.Column(
            "provision_confirm_token_hash",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "provision_action", sqlmodel.sql.sqltypes.AutoString(), nullable=True,
        ),
        sa.Column("delete_requested_at", sa.DateTime(), nullable=True),
        sa.Column(
            "delete_confirm_token_hash",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column("last_seen_at", sa.DateTime(), nullable=True),
        sa.Column("is_board_lead", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_agents_agent_token_hash"),
        "agents",
        ["agent_token_hash"],
        unique=False,
    )
    op.create_index(op.f("ix_agents_board_id"), "agents", ["board_id"], unique=False)
    op.create_index(
        op.f("ix_agents_delete_confirm_token_hash"),
        "agents",
        ["delete_confirm_token_hash"],
        unique=False,
    )
    op.create_index(
        op.f("ix_agents_is_board_lead"), "agents", ["is_board_lead"], unique=False,
    )
    op.create_index(op.f("ix_agents_name"), "agents", ["name"], unique=False)
    op.create_index(
        op.f("ix_agents_openclaw_session_id"),
        "agents",
        ["openclaw_session_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_agents_provision_action"),
        "agents",
        ["provision_action"],
        unique=False,
    )
    op.create_index(
        op.f("ix_agents_provision_confirm_token_hash"),
        "agents",
        ["provision_confirm_token_hash"],
        unique=False,
    )
    op.create_index(op.f("ix_agents_status"), "agents", ["status"], unique=False)


def _upgrade_part_3() -> None:
    op.create_table(
        "board_memory",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("content", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("tags", sa.JSON(), nullable=True),
        sa.Column("is_chat", sa.Boolean(), nullable=False),
        sa.Column("source", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_board_memory_board_id"), "board_memory", ["board_id"], unique=False,
    )
    op.create_index(
        op.f("ix_board_memory_is_chat"), "board_memory", ["is_chat"], unique=False,
    )
    op.create_table(
        "board_onboarding_sessions",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("session_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("messages", sa.JSON(), nullable=True),
        sa.Column("draft_goal", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_board_onboarding_sessions_board_id"),
        "board_onboarding_sessions",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_board_onboarding_sessions_status"),
        "board_onboarding_sessions",
        ["status"],
        unique=False,
    )
    op.create_table(
        "organization_board_access",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_member_id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("can_read", sa.Boolean(), nullable=False),
        sa.Column("can_write", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_member_id"],
            ["organization_members.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_member_id",
            "board_id",
            name="uq_org_board_access_member_board",
        ),
    )
    op.create_index(
        op.f("ix_organization_board_access_board_id"),
        "organization_board_access",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_board_access_organization_member_id"),
        "organization_board_access",
        ["organization_member_id"],
        unique=False,
    )
    op.create_table(
        "organization_invite_board_access",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_invite_id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("can_read", sa.Boolean(), nullable=False),
        sa.Column("can_write", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_invite_id"],
            ["organization_invites.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_invite_id",
            "board_id",
            name="uq_org_invite_board_access_invite_board",
        ),
    )
    op.create_index(
        op.f("ix_organization_invite_board_access_board_id"),
        "organization_invite_board_access",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_invite_board_access_organization_invite_id"),
        "organization_invite_board_access",
        ["organization_invite_id"],
        unique=False,
    )
    op.create_table(
        "tasks",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=True),
        sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("priority", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("due_at", sa.DateTime(), nullable=True),
        sa.Column("in_progress_at", sa.DateTime(), nullable=True),
        sa.Column("created_by_user_id", sa.Uuid(), nullable=True),
        sa.Column("assigned_agent_id", sa.Uuid(), nullable=True),
        sa.Column("auto_created", sa.Boolean(), nullable=False),
        sa.Column("auto_reason", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["assigned_agent_id"],
            ["agents.id"],
        ),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["created_by_user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_tasks_assigned_agent_id"),
        "tasks",
        ["assigned_agent_id"],
        unique=False,
    )
    op.create_index(op.f("ix_tasks_board_id"), "tasks", ["board_id"], unique=False)
    op.create_index(
        op.f("ix_tasks_created_by_user_id"),
        "tasks",
        ["created_by_user_id"],
        unique=False,
    )
    op.create_index(op.f("ix_tasks_priority"), "tasks", ["priority"], unique=False)
    op.create_index(op.f("ix_tasks_status"), "tasks", ["status"], unique=False)


def _upgrade_part_4() -> None:
    op.create_table(
        "activity_events",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("event_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("message", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("agent_id", sa.Uuid(), nullable=True),
        sa.Column("task_id", sa.Uuid(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["agent_id"],
            ["agents.id"],
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_activity_events_agent_id"),
        "activity_events",
        ["agent_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_activity_events_event_type"),
        "activity_events",
        ["event_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_activity_events_task_id"),
        "activity_events",
        ["task_id"],
        unique=False,
    )
    op.create_table(
        "approvals",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("task_id", sa.Uuid(), nullable=True),
        sa.Column("agent_id", sa.Uuid(), nullable=True),
        sa.Column("action_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("payload", sa.JSON(), nullable=True),
        sa.Column("confidence", sa.Integer(), nullable=False),
        sa.Column("rubric_scores", sa.JSON(), nullable=True),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("resolved_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["agent_id"],
            ["agents.id"],
        ),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_approvals_agent_id"), "approvals", ["agent_id"], unique=False,
    )
    op.create_index(
        op.f("ix_approvals_board_id"), "approvals", ["board_id"], unique=False,
    )
    op.create_index(op.f("ix_approvals_status"), "approvals", ["status"], unique=False)
    op.create_index(
        op.f("ix_approvals_task_id"), "approvals", ["task_id"], unique=False,
    )
    op.create_table(
        "task_dependencies",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("task_id", sa.Uuid(), nullable=False),
        sa.Column("depends_on_task_id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.CheckConstraint(
            "task_id <> depends_on_task_id", name="ck_task_dependencies_no_self",
        ),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["depends_on_task_id"],
            ["tasks.id"],
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "task_id",
            "depends_on_task_id",
            name="uq_task_dependencies_task_id_depends_on_task_id",
        ),
    )
    op.create_index(
        op.f("ix_task_dependencies_board_id"),
        "task_dependencies",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_task_dependencies_depends_on_task_id"),
        "task_dependencies",
        ["depends_on_task_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_task_dependencies_task_id"),
        "task_dependencies",
        ["task_id"],
        unique=False,
    )
    op.create_table(
        "task_fingerprints",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column(
            "fingerprint_hash", sqlmodel.sql.sqltypes.AutoString(), nullable=False,
        ),
        sa.Column("task_id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["board_id"],
            ["boards.id"],
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_task_fingerprints_board_id"),
        "task_fingerprints",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_task_fingerprints_fingerprint_hash"),
        "task_fingerprints",
        ["fingerprint_hash"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Drop initial schema objects."""
    _downgrade_part_1()
    _downgrade_part_2()
    _downgrade_part_3()


def _downgrade_part_1() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_task_fingerprints_fingerprint_hash"), table_name="task_fingerprints",
    )
    op.drop_index(op.f("ix_task_fingerprints_board_id"), table_name="task_fingerprints")
    op.drop_table("task_fingerprints")
    op.drop_index(op.f("ix_task_dependencies_task_id"), table_name="task_dependencies")
    op.drop_index(
        op.f("ix_task_dependencies_depends_on_task_id"), table_name="task_dependencies",
    )
    op.drop_index(op.f("ix_task_dependencies_board_id"), table_name="task_dependencies")
    op.drop_table("task_dependencies")
    op.drop_index(op.f("ix_approvals_task_id"), table_name="approvals")
    op.drop_index(op.f("ix_approvals_status"), table_name="approvals")
    op.drop_index(op.f("ix_approvals_board_id"), table_name="approvals")
    op.drop_index(op.f("ix_approvals_agent_id"), table_name="approvals")
    op.drop_table("approvals")
    op.drop_index(op.f("ix_activity_events_task_id"), table_name="activity_events")
    op.drop_index(op.f("ix_activity_events_event_type"), table_name="activity_events")
    op.drop_index(op.f("ix_activity_events_agent_id"), table_name="activity_events")
    op.drop_table("activity_events")
    op.drop_index(op.f("ix_tasks_status"), table_name="tasks")
    op.drop_index(op.f("ix_tasks_priority"), table_name="tasks")
    op.drop_index(op.f("ix_tasks_created_by_user_id"), table_name="tasks")
    op.drop_index(op.f("ix_tasks_board_id"), table_name="tasks")
    op.drop_index(op.f("ix_tasks_assigned_agent_id"), table_name="tasks")
    op.drop_table("tasks")
    op.drop_index(
        op.f("ix_organization_invite_board_access_organization_invite_id"),
        table_name="organization_invite_board_access",
    )
    op.drop_index(
        op.f("ix_organization_invite_board_access_board_id"),
        table_name="organization_invite_board_access",
    )
    op.drop_table("organization_invite_board_access")
    op.drop_index(
        op.f("ix_organization_board_access_organization_member_id"),
        table_name="organization_board_access",
    )
    op.drop_index(
        op.f("ix_organization_board_access_board_id"),
        table_name="organization_board_access",
    )
    op.drop_table("organization_board_access")
    op.drop_index(
        op.f("ix_board_onboarding_sessions_status"),
        table_name="board_onboarding_sessions",
    )
    op.drop_index(
        op.f("ix_board_onboarding_sessions_board_id"),
        table_name="board_onboarding_sessions",
    )
    op.drop_table("board_onboarding_sessions")
    op.drop_index(op.f("ix_board_memory_is_chat"), table_name="board_memory")
    op.drop_index(op.f("ix_board_memory_board_id"), table_name="board_memory")
    op.drop_table("board_memory")


def _downgrade_part_2() -> None:
    op.drop_index(op.f("ix_agents_status"), table_name="agents")
    op.drop_index(op.f("ix_agents_provision_confirm_token_hash"), table_name="agents")
    op.drop_index(op.f("ix_agents_provision_action"), table_name="agents")
    op.drop_index(op.f("ix_agents_openclaw_session_id"), table_name="agents")
    op.drop_index(op.f("ix_agents_name"), table_name="agents")
    op.drop_index(op.f("ix_agents_is_board_lead"), table_name="agents")
    op.drop_index(op.f("ix_agents_delete_confirm_token_hash"), table_name="agents")
    op.drop_index(op.f("ix_agents_board_id"), table_name="agents")
    op.drop_index(op.f("ix_agents_agent_token_hash"), table_name="agents")
    op.drop_table("agents")
    op.drop_index(
        op.f("ix_organization_members_user_id"), table_name="organization_members",
    )
    op.drop_index(
        op.f("ix_organization_members_role"), table_name="organization_members",
    )
    op.drop_index(
        op.f("ix_organization_members_organization_id"),
        table_name="organization_members",
    )
    op.drop_table("organization_members")
    op.drop_index(
        op.f("ix_organization_invites_token"), table_name="organization_invites",
    )
    op.drop_index(
        op.f("ix_organization_invites_role"), table_name="organization_invites",
    )
    op.drop_index(
        op.f("ix_organization_invites_organization_id"),
        table_name="organization_invites",
    )
    op.drop_index(
        op.f("ix_organization_invites_invited_email"),
        table_name="organization_invites",
    )
    op.drop_index(
        op.f("ix_organization_invites_created_by_user_id"),
        table_name="organization_invites",
    )
    op.drop_index(
        op.f("ix_organization_invites_accepted_by_user_id"),
        table_name="organization_invites",
    )
    op.drop_table("organization_invites")
    op.drop_index(op.f("ix_boards_slug"), table_name="boards")
    op.drop_index(op.f("ix_boards_organization_id"), table_name="boards")
    op.drop_index(op.f("ix_boards_gateway_id"), table_name="boards")
    op.drop_index(op.f("ix_boards_board_type"), table_name="boards")
    op.drop_index(op.f("ix_boards_board_group_id"), table_name="boards")
    op.drop_table("boards")


def _downgrade_part_3() -> None:
    op.drop_index(
        op.f("ix_board_group_memory_is_chat"), table_name="board_group_memory",
    )
    op.drop_index(
        op.f("ix_board_group_memory_board_group_id"), table_name="board_group_memory",
    )
    op.drop_table("board_group_memory")
    op.drop_index(op.f("ix_users_email"), table_name="users")
    op.drop_index(op.f("ix_users_clerk_user_id"), table_name="users")
    op.drop_index(op.f("ix_users_active_organization_id"), table_name="users")
    op.drop_table("users")
    op.drop_index(op.f("ix_gateways_organization_id"), table_name="gateways")
    op.drop_table("gateways")
    op.drop_index(op.f("ix_board_groups_slug"), table_name="board_groups")
    op.drop_index(op.f("ix_board_groups_organization_id"), table_name="board_groups")
    op.drop_table("board_groups")
    op.drop_index(op.f("ix_organizations_name"), table_name="organizations")
    op.drop_table("organizations")
    # ### end Alembic commands ###
