defmodule Towerops.Snmp.StorageReading do
  @moduledoc """
  Time-series storage reading schema.

  Stores individual storage readings (used/total bytes, usage percent, timestamp)
  collected during SNMP polling cycles.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias Towerops.Snmp.Storage

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "snmp_storage_readings" do
    field :used_bytes, :integer
    field :total_bytes, :integer
    field :usage_percent, :float
    field :checked_at, :utc_datetime

    belongs_to :storage, Storage

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

  @type t :: %__MODULE__{
          id: Ecto.UUID.t(),
          used_bytes: integer() | nil,
          total_bytes: integer() | nil,
          usage_percent: float() | nil,
          checked_at: DateTime.t(),
          storage_id: Ecto.UUID.t(),
          storage: Ecto.Association.NotLoaded.t() | Storage.t(),
          inserted_at: DateTime.t()
        }

  @doc false
  def changeset(reading, attrs) do
    reading
    |> cast(attrs, [:storage_id, :used_bytes, :total_bytes, :usage_percent, :checked_at])
    |> validate_required([:storage_id, :checked_at])
    |> foreign_key_constraint(:storage_id)
  end
end
