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

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

  schema "messages" do
    field :body, :string
    field :read_at, :utc_datetime
    belongs_to :assignment, WhoNeedHelp.Help.Assignment
    belongs_to :sender, WhoNeedHelp.Accounts.User
    timestamps(type: :utc_datetime)
  end

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