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

  def change do
    # Composite index for agent assignment queries that filter by both agent_token_id and enabled
    # This improves queries like: WHERE agent_token_id = ? AND enabled = true
    create index(:agent_assignments, [:agent_token_id, :enabled])

    # Composite index for filtering agents by organization and enabled status
    # This improves queries like: WHERE organization_id = ? AND enabled = true
    create index(:agent_tokens, [:organization_id, :enabled])

    # Composite index for finding online agents (organization + last_seen_at)
    # This improves queries that filter by org and sort by last_seen_at
    create index(:agent_tokens, [:organization_id, :last_seen_at])

    # Composite index for equipment assignment queries (equipment_id + enabled)
    # if device has an active assignment
    create index(:agent_assignments, [:equipment_id, :enabled])
  end
end
