"""XP-1145 - Add  indexes for rule result detail elements

Revision ID: fb76ac2d7a14
Revises: f955f6625002
Create Date: 2024-10-21 17:23:10.455614

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "fb76ac2d7a14"
down_revision = "f955f6625002"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index(
        op.f("ix_hallucination_claims_rule_result_detail_id"),
        "hallucination_claims",
        ["rule_result_detail_id"],
        unique=False,
    )
    op.drop_index("idx_inferences_task_id_created_at", table_name="inferences")
    op.create_index(
        "idx_inferences_task_id_created_at",
        "inferences",
        ["task_id", "created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_keyword_matches_rule_result_detail_id"),
        "keyword_matches",
        ["rule_result_detail_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_pii_entities_rule_result_detail_id"),
        "pii_entities",
        ["rule_result_detail_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_regex_matches_rule_result_detail_id"),
        "regex_matches",
        ["rule_result_detail_id"],
        unique=False,
    )
    op.create_index(
        "ix_rule_result_details_prompt_rule_result_id",
        "rule_result_details",
        ["prompt_rule_result_id"],
        unique=False,
    )
    op.create_index(
        "ix_rule_result_details_response_rule_result_id",
        "rule_result_details",
        ["response_rule_result_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_toxicity_scores_rule_result_detail_id"),
        "toxicity_scores",
        ["rule_result_detail_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_toxicity_scores_rule_result_detail_id"),
        table_name="toxicity_scores",
    )
    op.drop_index(
        "ix_rule_result_details_response_rule_result_id",
        table_name="rule_result_details",
    )
    op.drop_index(
        "ix_rule_result_details_prompt_rule_result_id",
        table_name="rule_result_details",
    )
    op.drop_index(
        op.f("ix_regex_matches_rule_result_detail_id"),
        table_name="regex_matches",
    )
    op.drop_index(
        op.f("ix_pii_entities_rule_result_detail_id"),
        table_name="pii_entities",
    )
    op.drop_index(
        op.f("ix_keyword_matches_rule_result_detail_id"),
        table_name="keyword_matches",
    )
    op.drop_index("idx_inferences_task_id_created_at", table_name="inferences")
    op.create_index(
        "idx_inferences_task_id_created_at",
        "inferences",
        ["task_id", sa.text("created_at DESC")],
        unique=False,
    )
    op.drop_index(
        op.f("ix_hallucination_claims_rule_result_detail_id"),
        table_name="hallucination_claims",
    )
    # ### end Alembic commands ###
