"""Initial migration

Revision ID: 50e22ca490f9
Revises:
Create Date: 2024-08-23 00:45:53.922227

"""

from collections.abc import Sequence

import fastapi_users_db_sqlalchemy
import fastapi_users_db_sqlalchemy.generics
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "50e22ca490f9"
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! ###
    sa.Enum("BASIC", "ADMIN", name="userrole").create(op.get_bind())
    op.create_table(
        "case",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("workflow_id", sa.String(), nullable=False),
        sa.Column("case_title", sa.String(), nullable=False),
        sa.Column("payload", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("malice", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("priority", sa.String(), nullable=False),
        sa.Column("action", sa.String(), nullable=True),
        sa.Column("context", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_pkey"),
    )
    op.create_index(op.f("ix_case_id"), "case", ["id"], unique=True)
    op.create_table(
        "caseevent",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("workflow_id", sa.String(), nullable=False),
        sa.Column("case_id", sa.String(), nullable=False),
        sa.Column("initiator_role", sa.String(), nullable=False),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="caseevent_pkey"),
    )
    op.create_index(op.f("ix_caseevent_id"), "caseevent", ["id"], unique=True)
    op.create_table(
        "ownership",
        sa.Column("resource_id", sa.String(), nullable=False),
        sa.Column("resource_type", sa.String(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("owner_type", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("resource_id", name="ownership_pkey"),
    )
    op.create_table(
        "udfspec",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("namespace", sa.String(), nullable=False),
        sa.Column("key", sa.String(), nullable=False),
        sa.Column("version", sa.String(), nullable=True),
        sa.Column(
            "json_schema", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("meta", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="udfspec_pkey"),
    )
    op.create_index(op.f("ix_udfspec_id"), "udfspec", ["id"], unique=True)
    op.create_table(
        "user",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("email", sa.String(), nullable=False),
        sa.Column("hashed_password", sa.String(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("is_superuser", sa.Boolean(), nullable=False),
        sa.Column("is_verified", sa.Boolean(), nullable=False),
        sa.Column("first_name", sa.String(), nullable=True),
        sa.Column("last_name", sa.String(), nullable=True),
        sa.Column(
            "role",
            postgresql.ENUM("BASIC", "ADMIN", name="userrole", create_type=False),
            nullable=False,
        ),
        sa.Column("settings", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("id", name="user_pkey"),
    )
    op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True)
    op.create_table(
        "workspace",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("settings", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="workspace_pkey"),
        sa.UniqueConstraint("id", name="workspace_id_key"),
    )
    op.create_index(op.f("ix_workspace_name"), "workspace", ["name"], unique=True)
    op.create_table(
        "accesstoken",
        sa.Column("token", sa.String(length=43), nullable=False),
        sa.Column(
            "created_at",
            fastapi_users_db_sqlalchemy.generics.TIMESTAMPAware(timezone=True),
            nullable=False,
        ),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="accesstoken_user_id_fkey",
        ),
        sa.PrimaryKeyConstraint("token", name="accesstoken_pkey"),
    )
    op.create_index(
        op.f("ix_accesstoken_created_at"), "accesstoken", ["created_at"], unique=False
    )
    op.create_table(
        "caseaction",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("tag", sa.String(), nullable=False),
        sa.Column("value", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="caseaction_user_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="caseaction_pkey"),
    )
    op.create_index(op.f("ix_caseaction_id"), "caseaction", ["id"], unique=True)
    op.create_table(
        "casecontext",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("tag", sa.String(), nullable=False),
        sa.Column("value", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="casecontext_user_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="casecontext_pkey"),
    )
    op.create_index(op.f("ix_casecontext_id"), "casecontext", ["id"], unique=True)
    op.create_table(
        "membership",
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("workspace_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="membership_user_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["workspace_id"],
            ["workspace.id"],
            name="membership_workspace_id_fkey",
        ),
        sa.PrimaryKeyConstraint("user_id", "workspace_id", name="membership_pkey"),
    )
    op.create_table(
        "oauthaccount",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("oauth_name", sa.String(), nullable=False),
        sa.Column("access_token", sa.String(), nullable=False),
        sa.Column("expires_at", sa.Integer(), nullable=True),
        sa.Column("refresh_token", sa.String(), nullable=True),
        sa.Column("account_id", sa.String(), nullable=False),
        sa.Column("account_email", sa.String(), nullable=False),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="oauthaccount_user_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="oauthaccount_pkey"),
    )
    op.create_index(
        op.f("ix_oauthaccount_account_id"), "oauthaccount", ["account_id"], unique=False
    )
    op.create_index(
        op.f("ix_oauthaccount_oauth_name"), "oauthaccount", ["oauth_name"], unique=False
    )
    op.create_table(
        "secret",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("encrypted_keys", sa.LargeBinary(), nullable=False),
        sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("owner_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="secret_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="secret_pkey"),
    )
    op.create_index(op.f("ix_secret_id"), "secret", ["id"], unique=True)
    op.create_index(op.f("ix_secret_name"), "secret", ["name"], unique=False)
    op.create_table(
        "workflow",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("version", sa.Integer(), nullable=True),
        sa.Column("entrypoint", sa.String(), nullable=True),
        sa.Column(
            "static_inputs", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("returns", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("object", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("icon_url", sa.String(), nullable=True),
        sa.Column("owner_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="workflow_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="workflow_pkey"),
    )
    op.create_index(op.f("ix_workflow_id"), "workflow", ["id"], unique=True)
    op.create_table(
        "action",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("inputs", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column(
            "control_flow", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("workflow_id", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflow.id"],
            ondelete="CASCADE",
            name="action_workflow_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="action_pkey"),
    )
    op.create_index(op.f("ix_action_id"), "action", ["id"], unique=True)
    op.create_table(
        "schedule",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("cron", sa.String(), nullable=True),
        sa.Column("inputs", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("every", sa.Interval(), nullable=False),
        sa.Column("offset", sa.Interval(), nullable=True),
        sa.Column("start_at", sa.DateTime(), nullable=True),
        sa.Column("end_at", sa.DateTime(), nullable=True),
        sa.Column("workflow_id", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflow.id"],
            ondelete="CASCADE",
            name="schedule_workflow_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="schedule_pkey"),
    )
    op.create_index(op.f("ix_schedule_id"), "schedule", ["id"], unique=True)
    op.create_table(
        "webhook",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("method", sa.String(), nullable=False),
        sa.Column("filters", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("workflow_id", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflow.id"],
            ondelete="CASCADE",
            name="webhook_workflow_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="webhook_pkey"),
    )
    op.create_index(op.f("ix_webhook_id"), "webhook", ["id"], unique=True)
    op.create_table(
        "workflowdefinition",
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("(now() AT TIME ZONE 'utc'::text)"),
            nullable=False,
        ),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("workflow_id", sa.String(), nullable=True),
        sa.Column("content", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflow.id"],
            ondelete="CASCADE",
            name="workflowdefinition_workflow_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="workflowdefinition_pkey"),
    )
    op.create_index(
        op.f("ix_workflowdefinition_id"), "workflowdefinition", ["id"], unique=True
    )
    op.create_index(
        op.f("ix_workflowdefinition_version"),
        "workflowdefinition",
        ["version"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_workflowdefinition_version"), table_name="workflowdefinition"
    )
    op.drop_index(op.f("ix_workflowdefinition_id"), table_name="workflowdefinition")
    op.drop_table("workflowdefinition")
    op.drop_index(op.f("ix_webhook_id"), table_name="webhook")
    op.drop_table("webhook")
    op.drop_index(op.f("ix_schedule_id"), table_name="schedule")
    op.drop_table("schedule")
    op.drop_index(op.f("ix_action_id"), table_name="action")
    op.drop_table("action")
    op.drop_index(op.f("ix_workflow_id"), table_name="workflow")
    op.drop_table("workflow")
    op.drop_index(op.f("ix_secret_name"), table_name="secret")
    op.drop_index(op.f("ix_secret_id"), table_name="secret")
    op.drop_table("secret")
    op.drop_index(op.f("ix_oauthaccount_oauth_name"), table_name="oauthaccount")
    op.drop_index(op.f("ix_oauthaccount_account_id"), table_name="oauthaccount")
    op.drop_table("oauthaccount")
    op.drop_table("membership")
    op.drop_index(op.f("ix_casecontext_id"), table_name="casecontext")
    op.drop_table("casecontext")
    op.drop_index(op.f("ix_caseaction_id"), table_name="caseaction")
    op.drop_table("caseaction")
    op.drop_index(op.f("ix_accesstoken_created_at"), table_name="accesstoken")
    op.drop_table("accesstoken")
    op.drop_index(op.f("ix_workspace_name"), table_name="workspace")
    op.drop_table("workspace")
    op.drop_index(op.f("ix_user_email"), table_name="user")
    op.drop_table("user")
    op.drop_index(op.f("ix_udfspec_id"), table_name="udfspec")
    op.drop_table("udfspec")
    op.drop_table("ownership")
    op.drop_index(op.f("ix_caseevent_id"), table_name="caseevent")
    op.drop_table("caseevent")
    op.drop_index(op.f("ix_case_id"), table_name="case")
    op.drop_table("case")
    sa.Enum("BASIC", "ADMIN", name="userrole").drop(op.get_bind())
    # ### end Alembic commands ###
