defmodule Towerops.Monitoring.Check do
  @moduledoc """
  Monitoring check schema for device reachability status.

  Records the result of each monitoring check (success/failure) with latency.
  """
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "monitoring_checks" do
    field :status, Ecto.Enum, values: [:success, :failure]
    field :response_time_ms, :float
    field :checked_at, :utc_datetime

    belongs_to :device, Towerops.Devices.Device

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

  @doc false
  def changeset(check, attrs) do
    check
    |> cast(attrs, [:device_id, :status, :response_time_ms, :checked_at])
    |> validate_required([:device_id, :status, :checked_at])
    |> foreign_key_constraint(:device_id)
  end
end
