"""add_performance_indexes

Revision ID: b018perfidx
Revises: b017scheduling
Create Date: 2026-02-10 06:00:00.000000

"""
from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "b018perfidx"
down_revision: Union[str, None] = "b017scheduling"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Composite index for detection rules filtered by technique + source
    op.create_index(
        "ix_detection_rules_technique_source",
        "detection_rules",
        ["mitre_technique_id", "source"],
    )

    # Composite index for snapshot technique states
    op.create_index(
        "ix_snapshot_technique_states_snap_tech",
        "snapshot_technique_states",
        ["snapshot_id", "technique_id"],
        unique=True,
    )

    # Covering index for tests frequently filtered by technique + state
    op.create_index(
        "ix_tests_technique_state",
        "tests",
        ["technique_id", "state"],
    )

    # Audit logs — timestamp-based lookups
    op.create_index(
        "ix_audit_logs_timestamp",
        "audit_logs",
        ["timestamp"],
    )

    # Audit logs — entity lookups
    op.create_index(
        "ix_audit_logs_entity",
        "audit_logs",
        ["entity_type", "entity_id"],
    )

    # Test detection results — triggered flag for maturity queries
    op.create_index(
        "ix_test_detection_results_triggered",
        "test_detection_results",
        ["triggered"],
    )

    # Compliance control mappings — composite for joins
    op.create_index(
        "ix_compliance_mappings_control_technique",
        "compliance_control_mappings",
        ["compliance_control_id", "technique_id"],
    )


def downgrade() -> None:
    op.drop_index("ix_compliance_mappings_control_technique", table_name="compliance_control_mappings")
    op.drop_index("ix_test_detection_results_triggered", table_name="test_detection_results")
    op.drop_index("ix_audit_logs_entity", table_name="audit_logs")
    op.drop_index("ix_audit_logs_timestamp", table_name="audit_logs")
    op.drop_index("ix_tests_technique_state", table_name="tests")
    op.drop_index("ix_snapshot_technique_states_snap_tech", table_name="snapshot_technique_states")
    op.drop_index("ix_detection_rules_technique_source", table_name="detection_rules")
