defmodule Towerops.Repo.Migrations.AddAlertsEquipmentIndexes do
  use Ecto.Migration

  def change do
    # Composite index for active alerts queries - filters by type and resolved status
    # Used by list_organization_active_alerts/1 and get_active_alert/2
    # Partial index only on unresolved alerts for efficiency
    create_if_not_exists index(:alerts, [:equipment_id, :alert_type, :resolved_at],
                           where: "resolved_at IS NULL",
                           name: :idx_alerts_equipment_type_unresolved
                         )

    # Composite index for SNMP-enabled equipment polling queries
    # Used by list_snmp_enabled_equipment/0 and agent polling target queries
    # Partial index only on SNMP-enabled equipment
    create_if_not_exists index(:equipment, [:snmp_enabled, :last_snmp_poll_at],
                           where: "snmp_enabled = true",
                           name: :idx_equipment_snmp_poll
                         )

    # Composite index for monitoring-enabled equipment with SNMP
    # Covers common query pattern: WHERE snmp_enabled AND monitoring_enabled
    create_if_not_exists index(:equipment, [:snmp_enabled, :monitoring_enabled, :site_id],
                           where: "snmp_enabled = true AND monitoring_enabled = true",
                           name: :idx_equipment_snmp_monitoring_site
                         )
  end
end
