"""Add Case and CaseFields schemas

Revision ID: 1ccd61edfcaa
Revises: 9039a443701d
Create Date: 2025-04-07 14:37:51.063412

"""

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 = "1ccd61edfcaa"
down_revision: str | None = "9039a443701d"
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(
        "UNKNOWN",
        "NEW",
        "IN_PROGRESS",
        "ON_HOLD",
        "RESOLVED",
        "CLOSED",
        "OTHER",
        name="casestatus",
    ).create(op.get_bind())
    sa.Enum(
        "UNKNOWN",
        "INFORMATIONAL",
        "LOW",
        "MEDIUM",
        "HIGH",
        "CRITICAL",
        "FATAL",
        "OTHER",
        name="caseseverity",
    ).create(op.get_bind())
    sa.Enum(
        "UNKNOWN", "LOW", "MEDIUM", "HIGH", "CRITICAL", "OTHER", name="casepriority"
    ).create(op.get_bind())
    op.create_table(
        "cases",
        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_number",
            sa.Integer(),
            sa.Identity(always=False, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("summary", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column(
            "priority",
            postgresql.ENUM(
                "UNKNOWN",
                "LOW",
                "MEDIUM",
                "HIGH",
                "CRITICAL",
                "OTHER",
                name="casepriority",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column(
            "severity",
            postgresql.ENUM(
                "UNKNOWN",
                "INFORMATIONAL",
                "LOW",
                "MEDIUM",
                "HIGH",
                "CRITICAL",
                "FATAL",
                "OTHER",
                name="caseseverity",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column(
            "status",
            postgresql.ENUM(
                "UNKNOWN",
                "NEW",
                "IN_PROGRESS",
                "ON_HOLD",
                "RESOLVED",
                "CLOSED",
                "OTHER",
                name="casestatus",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="cases_pkey"),
    )
    op.create_index(op.f("ix_cases_case_number"), "cases", ["case_number"], unique=True)
    op.create_index(op.f("ix_cases_id"), "cases", ["id"], unique=True)
    op.create_table(
        "case_fields",
        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("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("case_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_fields_case_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="case_fields_pkey"),
        sa.UniqueConstraint("case_id", name="case_fields_case_id_key"),
    )
    op.create_index(op.f("ix_case_fields_id"), "case_fields", ["id"], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_case_fields_id"), table_name="case_fields")
    op.drop_table("case_fields")
    op.drop_index(op.f("ix_cases_id"), table_name="cases")
    op.drop_index(op.f("ix_cases_case_number"), table_name="cases")
    op.drop_table("cases")
    sa.Enum(
        "UNKNOWN", "LOW", "MEDIUM", "HIGH", "CRITICAL", "OTHER", name="casepriority"
    ).drop(op.get_bind())
    sa.Enum(
        "UNKNOWN",
        "INFORMATIONAL",
        "LOW",
        "MEDIUM",
        "HIGH",
        "CRITICAL",
        "FATAL",
        "OTHER",
        name="caseseverity",
    ).drop(op.get_bind())
    sa.Enum(
        "UNKNOWN",
        "NEW",
        "IN_PROGRESS",
        "ON_HOLD",
        "RESOLVED",
        "CLOSED",
        "OTHER",
        name="casestatus",
    ).drop(op.get_bind())
    # ### end Alembic commands ###
