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

  import Ecto.Changeset

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

  schema "hrrr_profiles" do
    field :valid_time, :utc_datetime
    field :lat, :float
    field :lon, :float
    field :run_time, :utc_datetime
    field :profile, {:array, :map}
    field :hpbl_m, :float
    field :pwat_mm, :float
    field :surface_temp_c, :float
    field :surface_dewpoint_c, :float
    field :surface_pressure_mb, :float
    field :surface_refractivity, :float
    field :min_refractivity_gradient, :float
    field :ducting_detected, :boolean, default: false
    field :duct_characteristics, {:array, :map}
    field :is_grid_point, :boolean, default: false

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(valid_time lat lon)a
  @optional_fields ~w(run_time profile hpbl_m pwat_mm surface_temp_c surface_dewpoint_c surface_pressure_mb surface_refractivity min_refractivity_gradient ducting_detected duct_characteristics is_grid_point)a

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