defmodule Microwaveprop.Propagation.RunTiming do
  @moduledoc """
  One row per forecast hour of a `PropagationGridWorker` chain, recording
  how long that hour's fetch + score + persist cycle took.

  Rows are keyed by `(run_time, forecast_hour)`. `run_time` is the HRRR
  model cycle and `forecast_hour` ∈ 0..18.
  """
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "propagation_run_timings" do
    field :run_time, :utc_datetime
    field :forecast_hour, :integer
    field :valid_time, :utc_datetime
    field :started_at, :utc_datetime_usec
    field :finished_at, :utc_datetime_usec
    field :duration_ms, :integer
    field :status, Ecto.Enum, values: [:ok, :failed]
    field :error, :string

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

  @required ~w(run_time forecast_hour valid_time started_at finished_at duration_ms status)a
  @optional ~w(error)a

  def changeset(row, attrs) do
    row
    |> cast(attrs, @required ++ @optional)
    |> validate_required(@required)
    |> validate_number(:forecast_hour, greater_than_or_equal_to: 0, less_than_or_equal_to: 18)
    |> validate_number(:duration_ms, greater_than_or_equal_to: 0)
    |> unique_constraint([:run_time, :forecast_hour])
  end
end
