defmodule Towerops.DeviceProfiles.MemPoolDefinition do
  @moduledoc """
  Schema for memory pool definitions.

  Defines OID mappings for discovering and monitoring memory 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_mempool_definitions" do
    field :index, :string
    field :descr, :string
    field :total_oid, :string
    field :used_oid, :string
    field :free_oid, :string
    field :percent_used_oid, :string
    field :precision, :integer, default: 1
    field :type, :string
    field :class, :string
    field :warn_percent, :integer

    belongs_to :device_profile, DeviceProfile

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(definition, attrs) do
    definition
    |> cast(attrs, [
      :index,
      :descr,
      :total_oid,
      :used_oid,
      :free_oid,
      :percent_used_oid,
      :precision,
      :type,
      :class,
      :warn_percent,
      :device_profile_id
    ])
    |> validate_required([:device_profile_id])
    |> foreign_key_constraint(:device_profile_id)
  end
end
