"""create case duration definitions

Revision ID: f04f005837c9
Revises: 7f4c4a2130b8
Create Date: 2025-10-09 10:49:06.738163

"""

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 = "f04f005837c9"
down_revision: str | None = "7f4c4a2130b8"
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("FIRST", "LAST", name="casedurationanchorselection").create(op.get_bind())
    op.create_table(
        "case_duration_definition",
        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("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=True),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column(
            "start_event_type",
            postgresql.ENUM(
                "CASE_CREATED",
                "CASE_UPDATED",
                "CASE_CLOSED",
                "CASE_REOPENED",
                "PRIORITY_CHANGED",
                "SEVERITY_CHANGED",
                "STATUS_CHANGED",
                "FIELDS_CHANGED",
                "ASSIGNEE_CHANGED",
                "ATTACHMENT_CREATED",
                "ATTACHMENT_DELETED",
                "PAYLOAD_CHANGED",
                name="caseeventtype",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("start_timestamp_path", sa.String(), nullable=False),
        sa.Column(
            "start_field_filters",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=False,
        ),
        sa.Column(
            "start_selection",
            postgresql.ENUM(
                "FIRST", "LAST", name="casedurationanchorselection", create_type=False
            ),
            nullable=False,
        ),
        sa.Column(
            "end_event_type",
            postgresql.ENUM(
                "CASE_CREATED",
                "CASE_UPDATED",
                "CASE_CLOSED",
                "CASE_REOPENED",
                "PRIORITY_CHANGED",
                "SEVERITY_CHANGED",
                "STATUS_CHANGED",
                "FIELDS_CHANGED",
                "ASSIGNEE_CHANGED",
                "ATTACHMENT_CREATED",
                "ATTACHMENT_DELETED",
                "PAYLOAD_CHANGED",
                name="caseeventtype",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("end_timestamp_path", sa.String(), nullable=False),
        sa.Column(
            "end_field_filters", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column(
            "end_selection",
            postgresql.ENUM(
                "FIRST", "LAST", name="casedurationanchorselection", create_type=False
            ),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="case_duration_definition_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_duration_definition_pkey"),
        sa.UniqueConstraint(
            "owner_id", "name", name="uq_case_duration_definition_owner_name"
        ),
    )
    op.create_index(
        op.f("ix_case_duration_definition_id"),
        "case_duration_definition",
        ["id"],
        unique=True,
    )
    op.create_index(
        op.f("ix_case_duration_definition_name"),
        "case_duration_definition",
        ["name"],
        unique=False,
    )
    op.drop_constraint("uq_prompt_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_runbook_alias_owner_id", "runbook", ["alias", "owner_id"]
    )
    op.drop_constraint("tag_name_owner_id_key", "tag", type_="unique")
    op.create_unique_constraint("uq_tag_name_owner", "tag", ["name", "owner_id"])
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint("uq_tag_name_owner", "tag", type_="unique")
    op.create_unique_constraint(
        "tag_name_owner_id_key",
        "tag",
        ["name", "owner_id"],
        postgresql_nulls_not_distinct=False,
    )
    op.drop_constraint("uq_runbook_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_prompt_alias_owner_id",
        "runbook",
        ["alias", "owner_id"],
        postgresql_nulls_not_distinct=False,
    )
    op.drop_index(
        op.f("ix_case_duration_definition_name"), table_name="case_duration_definition"
    )
    op.drop_index(
        op.f("ix_case_duration_definition_id"), table_name="case_duration_definition"
    )
    op.drop_table("case_duration_definition")
    sa.Enum("FIRST", "LAST", name="casedurationanchorselection").drop(op.get_bind())
    # ### end Alembic commands ###
