defmodule Towerops.Preseem.SubscriberMetric do
  @moduledoc """
  Time-series aggregate QoE metrics per Preseem access point.
  """
  use Ecto.Schema

  import Ecto.Changeset

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

  schema "preseem_subscriber_metrics" do
    field :avg_latency, :float
    field :avg_jitter, :float
    field :avg_loss, :float
    field :avg_throughput, :float
    field :p95_latency, :float
    field :subscriber_count, :integer
    field :recorded_at, :utc_datetime

    belongs_to :preseem_access_point, Towerops.Preseem.AccessPoint
  end

  def changeset(metric, attrs) do
    metric
    |> cast(attrs, [
      :preseem_access_point_id,
      :avg_latency,
      :avg_jitter,
      :avg_loss,
      :avg_throughput,
      :p95_latency,
      :subscriber_count,
      :recorded_at
    ])
    |> validate_required([:preseem_access_point_id, :recorded_at])
    |> foreign_key_constraint(:preseem_access_point_id)
  end
end
