"""delete entities/records tables

Revision ID: fe20e84914ad
Revises: 7694c8910510
Create Date: 2025-11-16 22:09:22.270664

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "fe20e84914ad"
down_revision: str | None = "7694c8910510"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """
    This migration drops the following tables and ALL their data:
    - entity
    - entity_field
    - entity_field_option
    - entity_record
    - case_record

    This is an irreversible operation. All entity and record data will be permanently deleted.
    Downgrade is not supported without manual data restoration.
    """
    # ### commands auto generated by Alembic - please adjust! ###
    # Drop in dependency order: child tables first, then parent tables
    op.drop_index("idx_case_record_case", table_name="case_record")
    op.drop_index("idx_case_record_case_entity", table_name="case_record")
    op.drop_index("idx_case_record_entity", table_name="case_record")
    op.drop_index("ix_case_record_id", table_name="case_record")
    op.drop_table("case_record")
    op.drop_index("idx_record_entity", table_name="entity_record")
    op.drop_index("idx_record_gin", table_name="entity_record", postgresql_using="gin")
    op.drop_index("ix_entity_record_id", table_name="entity_record")
    op.drop_table("entity_record")
    op.drop_table("entity_field_option")
    op.drop_table("entity_field")
    op.drop_table("entity")
    sa.Enum(
        "INTEGER",
        "NUMBER",
        "TEXT",
        "BOOL",
        "JSON",
        "DATETIME",
        "DATE",
        "SELECT",
        "MULTI_SELECT",
        name="fieldtype",
    ).drop(op.get_bind())
    # ### end Alembic commands ###


def downgrade() -> None:
    """
    Downgrade is not supported for this migration.

    The entity and record tables have been dropped along with all their data.
    To restore this functionality, manual intervention is required:

    """
    raise NotImplementedError(
        "Downgrade not supported: Entity and record tables were dropped with data loss. Manual restoration from backups is required."
    )
