"""initial migration

Revision ID: 89ab3b24b597
Revises:
Create Date: 2025-10-21 12:35:06.042436

"""

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "89ab3b24b597"
down_revision: str | None = None
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(
        "evaloutput",
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("flow", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("run_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("trigger_ev_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("output", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_evaloutput_flow"), "evaloutput", ["flow"], unique=False)
    op.create_index(
        op.f("ix_evaloutput_run_id"), "evaloutput", ["run_id"], unique=False
    )
    op.create_index(
        op.f("ix_evaloutput_status"), "evaloutput", ["status"], unique=False
    )
    op.create_index(
        op.f("ix_evaloutput_trigger_ev_id"),
        "evaloutput",
        ["trigger_ev_id"],
        unique=False,
    )
    op.create_table(
        "event",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("run_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("type", sa.String(), nullable=True),
        sa.Column("flow", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("node_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("data", sa.JSON(), nullable=True),
        sa.Column("ts", sa.BIGINT(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "idx_event_flow_node_id", "event", ["flow", "node_id"], unique=False
    )
    op.create_index(op.f("ix_event_flow"), "event", ["flow"], unique=False)
    op.create_index(op.f("ix_event_node_id"), "event", ["node_id"], unique=False)
    op.create_index(op.f("ix_event_run_id"), "event", ["run_id"], unique=False)
    op.create_index(op.f("ix_event_type"), "event", ["type"], unique=False)
    op.create_table(
        "node",
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("flow", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("type", sa.String(), nullable=True),
        sa.Column("source", sa.String(), nullable=True),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("dep_ids", sa.JSON(), nullable=True),
        sa.Column("handler", sa.String(), nullable=True),
        sa.Column("handler_input", sa.JSON(), nullable=True),
        sa.Column("filter", sa.JSON(), nullable=True),
        sa.Column("validator", sa.JSON(), nullable=True),
        sa.Column("conditions", sa.JSON(), nullable=True),
        sa.Column("additional_meta", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("idx_node_id_flow", "node", ["id", "flow"], unique=False)
    op.create_index(op.f("ix_node_flow"), "node", ["flow"], unique=False)
    op.create_index(op.f("ix_node_id"), "node", ["id"], unique=False)
    op.create_index(op.f("ix_node_source"), "node", ["source"], unique=False)
    op.create_index(op.f("ix_node_status"), "node", ["status"], unique=False)
    op.create_index(op.f("ix_node_type"), "node", ["type"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_node_type"), table_name="node")
    op.drop_index(op.f("ix_node_status"), table_name="node")
    op.drop_index(op.f("ix_node_source"), table_name="node")
    op.drop_index(op.f("ix_node_id"), table_name="node")
    op.drop_index(op.f("ix_node_flow"), table_name="node")
    op.drop_index("idx_node_id_flow", table_name="node")
    op.drop_table("node")
    op.drop_index(op.f("ix_event_type"), table_name="event")
    op.drop_index(op.f("ix_event_run_id"), table_name="event")
    op.drop_index(op.f("ix_event_node_id"), table_name="event")
    op.drop_index(op.f("ix_event_flow"), table_name="event")
    op.drop_index("idx_event_flow_node_id", table_name="event")
    op.drop_table("event")
    op.drop_index(op.f("ix_evaloutput_trigger_ev_id"), table_name="evaloutput")
    op.drop_index(op.f("ix_evaloutput_status"), table_name="evaloutput")
    op.drop_index(op.f("ix_evaloutput_run_id"), table_name="evaloutput")
    op.drop_index(op.f("ix_evaloutput_flow"), table_name="evaloutput")
    op.drop_table("evaloutput")
    # ### end Alembic commands ###
