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

  import Ecto.Changeset

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

  schema "nexrad_observations" do
    field :observed_at, :utc_datetime
    field :lat, :float
    field :lon, :float
    field :mean_reflectivity_dbz, :float
    field :max_reflectivity_dbz, :float
    field :texture_variance, :float
    field :pixel_count, :integer

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(observed_at lat lon)a
  @optional_fields ~w(mean_reflectivity_dbz max_reflectivity_dbz texture_variance pixel_count)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, :observed_at])
  end
end
