defmodule Towerops.Snmp.ProcessorReading do
  @moduledoc """
  Time-series processor reading schema.

  Stores individual processor load readings collected during SNMP polling cycles.
  Similar to SensorReading but specifically for processor/CPU load data.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.Snmp.Processor

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "snmp_processor_readings" do
    field :load_percent, :float
    field :status, :string
    field :checked_at, :utc_datetime

    belongs_to :processor, Processor

    timestamps(type: :utc_datetime, updated_at: false)
  end

  @type t :: %__MODULE__{
          id: Ecto.UUID.t(),
          load_percent: float() | nil,
          status: String.t(),
          checked_at: DateTime.t(),
          processor_id: Ecto.UUID.t(),
          processor: Ecto.Association.NotLoaded.t() | Processor.t(),
          inserted_at: DateTime.t()
        }

  @doc false
  def changeset(reading, attrs) do
    reading
    |> cast(attrs, [:processor_id, :load_percent, :status, :checked_at])
    |> validate_required([:processor_id, :status, :checked_at])
    |> validate_inclusion(:status, ["ok", "warning", "critical", "error"])
    |> foreign_key_constraint(:processor_id)
  end
end
