defmodule Microwaveprop.Qrz.Callsign do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

  @type t :: %__MODULE__{}

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "qrz_callsigns" do
    field :callsign, :string
    field :data, :map
    timestamps(type: :utc_datetime)
  end

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