defmodule Microwaveprop.Commercial.Link do
  @moduledoc false
  use Ecto.Schema

  import Ecto.Changeset

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

  schema "commercial_links" do
    field :label, :string
    field :host, :string
    field :community, :string
    field :radio_type, :string, default: "af11x"
    field :endpoint_a, :map
    field :endpoint_b, :map
    field :weather_station, :string
    field :enabled, :boolean, default: true

    timestamps(type: :utc_datetime)
  end

  @type t :: %__MODULE__{}

  @required_fields ~w(label host community radio_type)a
  @optional_fields ~w(endpoint_a endpoint_b weather_station enabled)a

  @spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
  def changeset(link, attrs) do
    link
    |> cast(attrs, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
    |> validate_inclusion(:radio_type, ~w(af11x af60))
    |> unique_constraint(:label)
  end
end
