"""add_case_records

Revision ID: 53678b07d2d1
Revises: 584c872028d2
Create Date: 2025-09-02 21:11:07.222860

"""

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 = "53678b07d2d1"
down_revision: str | None = "584c872028d2"
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(
        "entity_record",
        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("entity_id", sa.UUID(), nullable=False),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["entity_id"],
            ["entity.id"],
            ondelete="CASCADE",
            name="entity_record_entity_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="entity_record_pkey"),
        sa.UniqueConstraint("id", name="uq_entity_record_id"),
    )
    op.create_index("idx_record_entity", "entity_record", ["entity_id"], unique=False)
    op.create_index(
        "idx_record_gin",
        "entity_record",
        ["data"],
        unique=False,
        postgresql_using="gin",
    )
    op.create_index(op.f("ix_entity_record_id"), "entity_record", ["id"], unique=True)
    op.create_table(
        "case_record",
        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("entity_id", sa.UUID(), nullable=False),
        sa.Column("record_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_record_case_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["entity_id"],
            ["entity.id"],
            ondelete="CASCADE",
            name="case_record_entity_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["record_id"],
            ["entity_record.id"],
            ondelete="CASCADE",
            name="case_record_record_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="case_record_pkey"),
        sa.UniqueConstraint("case_id", "record_id", name="uq_case_record_link"),
    )
    op.create_index("idx_case_record_case", "case_record", ["case_id"], unique=False)
    op.create_index(
        "idx_case_record_case_entity",
        "case_record",
        ["case_id", "entity_id"],
        unique=False,
    )
    op.create_index(
        "idx_case_record_entity", "case_record", ["entity_id"], unique=False
    )
    op.create_index(op.f("ix_case_record_id"), "case_record", ["id"], unique=True)
    op.alter_column("entity", "owner_id", existing_type=sa.UUID(), nullable=True)
    op.drop_constraint("entity_field_owner_id_fkey", "entity_field", type_="foreignkey")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key(
        "entity_field_owner_id_fkey",
        "entity_field",
        "workspace",
        ["owner_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.alter_column("entity", "owner_id", existing_type=sa.UUID(), nullable=False)
    op.drop_index(op.f("ix_case_record_id"), table_name="case_record")
    op.drop_index("idx_case_record_entity", table_name="case_record")
    op.drop_index("idx_case_record_case_entity", table_name="case_record")
    op.drop_index("idx_case_record_case", table_name="case_record")
    op.drop_table("case_record")
    op.drop_index(op.f("ix_entity_record_id"), table_name="entity_record")
    op.drop_index("idx_record_gin", table_name="entity_record", postgresql_using="gin")
    op.drop_index("idx_record_entity", table_name="entity_record")
    op.drop_table("entity_record")
    # ### end Alembic commands ###
