defmodule WhoNeedHelp.Activities.Message do
  use Ecto.Schema
  import Ecto.Changeset

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

  schema "activity_messages" do
    field :body, :string
    belongs_to :activity, WhoNeedHelp.Activities.Activity
    belongs_to :sender, WhoNeedHelp.Accounts.User

    timestamps(type: :utc_datetime)
  end

  def changeset(message, attrs) do
    message
    |> cast(attrs, [:body, :activity_id, :sender_id])
    |> validate_required([:body, :activity_id, :sender_id])
    |> validate_length(:body, min: 1, max: 2_000)
  end
end
