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

  import Ecto.Changeset

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

  schema "weather_stations" do
    field :station_code, :string
    field :station_type, :string
    field :wmo_number, :integer
    field :name, :string
    field :lat, :float
    field :lon, :float
    field :elevation_m, :float
    field :state, :string

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(station_code station_type name lat lon)a
  @optional_fields ~w(wmo_number elevation_m state)a

  @spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
  def changeset(station, attrs) do
    station
    |> cast(attrs, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
    |> validate_inclusion(:station_type, ~w(asos sounding))
    |> unique_constraint([:station_code, :station_type])
  end
end
