defmodule Microwaveprop.SpaceWeather.GeomagneticObservation do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

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

  schema "geomagnetic_observations" do
    field :valid_time, :utc_datetime
    field :kp_index, :integer
    field :estimated_kp, :float

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(valid_time)a
  @optional_fields ~w(kp_index estimated_kp)a

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