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

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

  schema "activity_participants" do
    field :role, Ecto.Enum, values: [:organizer, :participant], default: :participant

    field :status, Ecto.Enum,
      values: [:requested, :approved, :declined, :left],
      default: :requested

    field :reviewed_at, :utc_datetime
    field :left_at, :utc_datetime
    belongs_to :activity, WhoNeedHelp.Activities.Activity
    belongs_to :user, WhoNeedHelp.Accounts.User

    timestamps(type: :utc_datetime)
  end

  def changeset(participant, attrs) do
    participant
    |> cast(attrs, [:activity_id, :user_id, :role, :status, :reviewed_at, :left_at])
    |> validate_required([:activity_id, :user_id, :role, :status])
    |> unique_constraint([:activity_id, :user_id])
  end
end
