"""baseline

Revision ID: 9e9fd5dc5c44
Revises:
Create Date: 2026-04-21 23:42:58.429526

"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel

import awaithumans.server.core.encryption  # noqa: F401  — used in column types below
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "9e9fd5dc5c44"
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(
        "audit_entries",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("task_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("from_status", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("to_status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("action", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("actor_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("actor_email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("channel", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("extra_data", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("audit_entries", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_audit_entries_task_id"), ["task_id"], unique=False)

    op.create_table(
        "email_sender_identities",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("display_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("from_email", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("from_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("reply_to", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("transport", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "transport_config", awaithumans.server.core.encryption.EncryptedString(), nullable=False
        ),
        sa.Column("verified", sa.Boolean(), nullable=False),
        sa.Column("verified_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "slack_installations",
        sa.Column("team_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("team_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("bot_user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "bot_token", awaithumans.server.core.encryption.EncryptedString(), nullable=False
        ),
        sa.Column("scopes", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("enterprise_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("installed_by_user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("installed_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("team_id"),
    )
    with op.batch_alter_table("slack_installations", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_slack_installations_enterprise_id"), ["enterprise_id"], unique=False
        )

    op.create_table(
        "tasks",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("idempotency_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("task", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("payload", sa.JSON(), nullable=True),
        sa.Column("payload_schema", sa.JSON(), nullable=True),
        sa.Column("response_schema", sa.JSON(), nullable=True),
        sa.Column("form_definition", sa.JSON(), nullable=True),
        sa.Column(
            "status",
            sa.Enum(
                "CREATED",
                "NOTIFIED",
                "ASSIGNED",
                "IN_PROGRESS",
                "SUBMITTED",
                "VERIFIED",
                "COMPLETED",
                "REJECTED",
                "TIMED_OUT",
                "CANCELLED",
                "VERIFICATION_EXHAUSTED",
                name="taskstatus",
            ),
            nullable=False,
        ),
        sa.Column("assign_to", sa.JSON(), nullable=True),
        sa.Column("assigned_to_email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("notify", sa.JSON(), nullable=True),
        sa.Column("response", sa.JSON(), nullable=True),
        sa.Column("verifier_config", sa.JSON(), nullable=True),
        sa.Column("verifier_result", sa.JSON(), nullable=True),
        sa.Column("verification_attempt", sa.Integer(), nullable=False),
        sa.Column("timeout_seconds", sa.Integer(), nullable=False),
        sa.Column("redact_payload", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
        sa.Column("timed_out_at", sa.DateTime(), nullable=True),
        sa.Column("timeout_at", sa.DateTime(), nullable=True),
        sa.Column("callback_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("completed_by_email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("completed_via_channel", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("tasks", schema=None) as batch_op:
        batch_op.create_index(
            "ix_tasks_active_idempotency_key",
            ["idempotency_key"],
            unique=True,
            sqlite_where=sa.text(
                "status NOT IN ('CANCELLED', 'COMPLETED', 'TIMED_OUT', 'VERIFICATION_EXHAUSTED')"
            ),
            postgresql_where=sa.text(
                "status NOT IN ('CANCELLED', 'COMPLETED', 'TIMED_OUT', 'VERIFICATION_EXHAUSTED')"
            ),
        )
        batch_op.create_index(
            batch_op.f("ix_tasks_assigned_to_email"), ["assigned_to_email"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_tasks_idempotency_key"), ["idempotency_key"], unique=False
        )
        batch_op.create_index(batch_op.f("ix_tasks_timeout_at"), ["timeout_at"], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("tasks", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_tasks_timeout_at"))
        batch_op.drop_index(batch_op.f("ix_tasks_idempotency_key"))
        batch_op.drop_index(batch_op.f("ix_tasks_assigned_to_email"))
        batch_op.drop_index(
            "ix_tasks_active_idempotency_key",
            sqlite_where=sa.text(
                "status NOT IN ('CANCELLED', 'COMPLETED', 'TIMED_OUT', 'VERIFICATION_EXHAUSTED')"
            ),
            postgresql_where=sa.text(
                "status NOT IN ('CANCELLED', 'COMPLETED', 'TIMED_OUT', 'VERIFICATION_EXHAUSTED')"
            ),
        )

    op.drop_table("tasks")
    with op.batch_alter_table("slack_installations", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_slack_installations_enterprise_id"))

    op.drop_table("slack_installations")
    op.drop_table("email_sender_identities")
    with op.batch_alter_table("audit_entries", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_audit_entries_task_id"))

    op.drop_table("audit_entries")
    # ### end Alembic commands ###
