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

  import Ecto.Changeset

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

  schema "iemre_observations" do
    field :lat, :float
    field :lon, :float
    field :date, :date
    field :hourly, {:array, :map}, default: []

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(lat lon date)a
  @optional_fields ~w(hourly)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, :date])
  end
end
