"""tasks added to cases

Revision ID: b6c80a9bb5ce
Revises: af3134e6907d
Create Date: 2025-10-23 23:48:44.091444

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic_postgresql_enum import TableReference
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "b6c80a9bb5ce"
down_revision: str | None = "af3134e6907d"
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(
        "TODO", "IN_PROGRESS", "COMPLETED", "BLOCKED", name="casetaskstatus"
    ).create(op.get_bind())
    op.create_table(
        "case_tasks",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("case_id", sa.UUID(), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column(
            "priority",
            postgresql.ENUM(
                "UNKNOWN",
                "LOW",
                "MEDIUM",
                "HIGH",
                "CRITICAL",
                "OTHER",
                name="casepriority",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column(
            "status",
            postgresql.ENUM(
                "TODO",
                "IN_PROGRESS",
                "COMPLETED",
                "BLOCKED",
                name="casetaskstatus",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("assignee_id", sa.UUID(), nullable=True),
        sa.Column("workflow_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignee_id"],
            ["user.id"],
            ondelete="SET NULL",
            name="case_tasks_assignee_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_tasks_case_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflow.id"],
            ondelete="SET NULL",
            name="case_tasks_workflow_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_tasks_pkey"),
    )
    op.create_index(op.f("ix_case_tasks_id"), "case_tasks", ["id"], unique=True)
    op.sync_enum_values(  # type: ignore[attr-defined]
        enum_schema="public",
        enum_name="caseeventtype",
        new_values=[
            "CASE_CREATED",
            "CASE_UPDATED",
            "CASE_CLOSED",
            "CASE_REOPENED",
            "PRIORITY_CHANGED",
            "SEVERITY_CHANGED",
            "STATUS_CHANGED",
            "FIELDS_CHANGED",
            "ASSIGNEE_CHANGED",
            "ATTACHMENT_CREATED",
            "ATTACHMENT_DELETED",
            "PAYLOAD_CHANGED",
            "TASK_CREATED",
            "TASK_DELETED",
            "TASK_STATUS_CHANGED",
            "TASK_PRIORITY_CHANGED",
            "TASK_WORKFLOW_CHANGED",
            "TASK_ASSIGNEE_CHANGED",
        ],
        affected_columns=[
            TableReference(
                table_schema="public",
                table_name="case_duration_definition",
                column_name="end_event_type",
            ),
            TableReference(
                table_schema="public",
                table_name="case_duration_definition",
                column_name="start_event_type",
            ),
            TableReference(
                table_schema="public", table_name="case_event", column_name="type"
            ),
        ],
        enum_values_to_rename=[],
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.sync_enum_values(  # type: ignore[attr-defined]
        enum_schema="public",
        enum_name="caseeventtype",
        new_values=[
            "CASE_CREATED",
            "CASE_UPDATED",
            "CASE_CLOSED",
            "CASE_REOPENED",
            "PRIORITY_CHANGED",
            "SEVERITY_CHANGED",
            "STATUS_CHANGED",
            "FIELDS_CHANGED",
            "ASSIGNEE_CHANGED",
            "ATTACHMENT_CREATED",
            "ATTACHMENT_DELETED",
            "PAYLOAD_CHANGED",
        ],
        affected_columns=[
            TableReference(
                table_schema="public",
                table_name="case_duration_definition",
                column_name="end_event_type",
            ),
            TableReference(
                table_schema="public",
                table_name="case_duration_definition",
                column_name="start_event_type",
            ),
            TableReference(
                table_schema="public", table_name="case_event", column_name="type"
            ),
        ],
        enum_values_to_rename=[],
    )
    op.drop_index(op.f("ix_case_tasks_id"), table_name="case_tasks")
    op.drop_table("case_tasks")
    sa.Enum("TODO", "IN_PROGRESS", "COMPLETED", "BLOCKED", name="casetaskstatus").drop(
        op.get_bind()
    )
    # ### end Alembic commands ###
