defmodule Microwaveprop.CallsignLocation.Location do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

  @type t :: %__MODULE__{}

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "callsign_locations" do
    field :callsign, :string
    field :latitude, :float
    field :longitude, :float
    field :gridsquare, :string

    timestamps(type: :utc_datetime)
  end

  @spec changeset(%__MODULE__{}, map()) :: Ecto.Changeset.t()
  def changeset(location, attrs) do
    location
    |> cast(attrs, [:callsign, :latitude, :longitude, :gridsquare])
    |> validate_required([:callsign, :latitude, :longitude, :gridsquare])
    |> update_change(:callsign, &String.upcase/1)
    |> unique_constraint(:callsign)
  end
end
