defmodule Towerops.DeviceProfiles.ProcessorDefinition do
  @moduledoc """
  Schema for CPU/processor definitions.

  Defines OID mappings for discovering and monitoring CPU usage.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.DeviceProfiles.DeviceProfile

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "profile_processor_definitions" do
    field :oid, :string
    field :num_oid, :string
    field :index, :string
    field :descr, :string
    field :precision, :integer, default: 1
    field :type, :string
    field :warn_percent, :integer
    field :entPhysicalIndex, :string

    belongs_to :device_profile, DeviceProfile

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(definition, attrs) do
    definition
    |> cast(attrs, [
      :oid,
      :num_oid,
      :index,
      :descr,
      :precision,
      :type,
      :warn_percent,
      :entPhysicalIndex,
      :device_profile_id
    ])
    |> validate_required([:oid, :num_oid, :device_profile_id])
    |> foreign_key_constraint(:device_profile_id)
  end
end
