defmodule Microwaveprop.Weather.RtmaObservation do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

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

  schema "rtma_observations" do
    field :valid_time, :utc_datetime
    field :lat, :float
    field :lon, :float
    field :temp_c, :float
    field :dewpoint_c, :float
    field :pressure_mb, :float
    field :wind_u_ms, :float
    field :wind_v_ms, :float
    field :visibility_m, :float
    field :precip_mm, :float

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(valid_time lat lon)a
  @optional_fields ~w(temp_c dewpoint_c pressure_mb wind_u_ms wind_v_ms visibility_m precip_mm)a

  @spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
  def changeset(observation, attrs) do
    observation
    |> cast(attrs, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
    |> unique_constraint([:lat, :lon, :valid_time])
  end
end
