defmodule Mobilizon.Invitation do
  @moduledoc """
  Represents a local invitation to a group.
  """

  use Ecto.Schema
  import Ecto.Changeset
  alias Mobilizon.Actors.Actor

  schema "invitations" do
    # We need read_after_writes because the uuid is generated by
    # the database gen_random_uuid() function
    field :token, :string, read_after_writes: true

    # label can't be NULL in database
    # default: "" permit to set it to "" if label is nil
    field :label, :string, default: ""

    belongs_to :group, Actor

    timestamps()
  end

  def changeset(invitation, attrs) do
    invitation
    |> cast(attrs, [:label, :group_id])
    |> validate_required([:group_id])
  end
end