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

  import Ecto.Changeset

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

  schema "solar_xray_observations" do
    field :valid_time, :utc_datetime
    field :satellite, :integer
    field :flux_wm2, :float
    field :energy_band, :string
    field :electron_contaminated, :boolean, default: false

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(valid_time)a
  @optional_fields ~w(satellite flux_wm2 energy_band electron_contaminated)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, :energy_band])
  end
end
