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

  import Ecto.Changeset

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

  schema "solar_flux_observations" do
    field :valid_time, :utc_datetime
    field :frequency_mhz, :integer
    field :flux_sfu, :float
    field :reporting_schedule, :string
    field :ninety_day_mean, :float

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(valid_time)a
  @optional_fields ~w(frequency_mhz flux_sfu reporting_schedule ninety_day_mean)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
