"""add case duration table

Revision ID: d4fd132ccb50
Revises: f04f005837c9
Create Date: 2025-10-21 12:43:53.678129

"""

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 = "d4fd132ccb50"
down_revision: str | None = "f04f005837c9"
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(
        "case_duration",
        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("case_id", sa.UUID(), nullable=False),
        sa.Column("definition_id", sa.UUID(), nullable=False),
        sa.Column("start_event_id", sa.UUID(), nullable=True),
        sa.Column("end_event_id", sa.UUID(), nullable=True),
        sa.Column("started_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("ended_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("duration", sa.Interval(), nullable=True),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_duration_case_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["definition_id"],
            ["case_duration_definition.id"],
            ondelete="CASCADE",
            name="case_duration_definition_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["end_event_id"],
            ["case_event.id"],
            ondelete="SET NULL",
            name="case_duration_end_event_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="case_duration_owner_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["start_event_id"],
            ["case_event.id"],
            ondelete="SET NULL",
            name="case_duration_start_event_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_duration_pkey"),
        sa.UniqueConstraint(
            "case_id", "definition_id", name="uq_case_duration_case_definition"
        ),
    )
    op.create_index(op.f("ix_case_duration_id"), "case_duration", ["id"], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_case_duration_id"), table_name="case_duration")
    op.drop_table("case_duration")
    # ### end Alembic commands ###
