"""Add case events

Revision ID: fc82d8b66c20
Revises: 14b4de0bbc0e
Create Date: 2025-05-27 14:39:48.932612

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "fc82d8b66c20"
down_revision: str | None = "14b4de0bbc0e"
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(
        "CASE_CREATED",
        "CASE_UPDATED",
        "CASE_CLOSED",
        "CASE_REOPENED",
        "PRIORITY_CHANGED",
        "SEVERITY_CHANGED",
        "STATUS_CHANGED",
        "FIELDS_CHANGED",
        "ASSIGNEE_CHANGED",
        name="caseeventtype",
    ).create(op.get_bind())
    op.create_table(
        "case_event",
        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(
            "type",
            postgresql.ENUM(
                "CASE_CREATED",
                "CASE_UPDATED",
                "CASE_CLOSED",
                "CASE_REOPENED",
                "PRIORITY_CHANGED",
                "SEVERITY_CHANGED",
                "STATUS_CHANGED",
                "FIELDS_CHANGED",
                "ASSIGNEE_CHANGED",
                name="caseeventtype",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("case_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_event_case_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_event_pkey"),
    )
    op.create_index(op.f("ix_case_event_id"), "case_event", ["id"], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_case_event_id"), table_name="case_event")
    op.drop_table("case_event")
    sa.Enum(
        "CASE_CREATED",
        "CASE_UPDATED",
        "CASE_CLOSED",
        "CASE_REOPENED",
        "PRIORITY_CHANGED",
        "SEVERITY_CHANGED",
        "STATUS_CHANGED",
        "FIELDS_CHANGED",
        "ASSIGNEE_CHANGED",
        name="caseeventtype",
    ).drop(op.get_bind())
    # ### end Alembic commands ###
