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

  def up do
    # Add agent_token_id column to track which agent performed the check
    alter table(:monitoring_checks) do
      add :agent_token_id, references(:agent_tokens, type: :binary_id, on_delete: :nilify_all)
    end

    # Add index for efficient latency queries (agent + device + time range)
    create index(:monitoring_checks, [:agent_token_id, :device_id, :checked_at])

    # Add index for device-specific latency analysis
    create index(:monitoring_checks, [:device_id, :agent_token_id, :status, :checked_at])
  end

  def down do
    drop index(:monitoring_checks, [:device_id, :agent_token_id, :status, :checked_at])
    drop index(:monitoring_checks, [:agent_token_id, :device_id, :checked_at])

    alter table(:monitoring_checks) do
      remove :agent_token_id
    end
  end
end
