defmodule Mobilizon.GraphQL.Schema.ActivityType do
  @moduledoc """
  Schema representation for Activity
  """
  use Absinthe.Schema.Notation

  alias Mobilizon.Actors.{Actor, Member}
  alias Mobilizon.Discussions.{Comment, Discussion}
  alias Mobilizon.Events.Event
  alias Mobilizon.Posts.Post
  alias Mobilizon.Resources.Resource

  enum :activity_type do
    value(:event, description: "Activities concerning events")
    value(:post, description: "Activities concerning posts")
    value(:discussion, description: "Activities concerning discussions")
    value(:resource, description: "Activities concerning resources")
    value(:group, description: "Activities concerning group settings")
    value(:member, description: "Activities concerning members")
  end

  enum :activity_author do
    value(:self, description: "Activities created by the current actor")
    value(:by, description: "Activities created by others")
  end

  object :activity_param_item do
    meta(:authorize, :user)
    field(:key, :string)
    field(:value, :string)
  end

  interface :activity_object do
    meta(:authorize, :user)
    field(:id, :id)

    resolve_type(fn
      %Event{}, _ ->
        :event

      %Post{}, _ ->
        :post

      %Actor{type: "Group"}, _ ->
        :group

      %Member{}, _ ->
        :member

      %Resource{}, _ ->
        :resource

      %Discussion{}, _ ->
        :discussion

      %Comment{}, _ ->
        :comment

      %Actor{type: :Group}, _ ->
        :group

      _, _ ->
        nil
    end)
  end

  @desc """
  A paginated list of activity items
  """
  object :paginated_activity_list do
    meta(:authorize, :user)
    field(:elements, list_of(:activity), description: "A list of activities")
    field(:total, :integer, description: "The total number of elements in the list")
  end

  object :activity do
    meta(:authorize, :user)
    field(:id, :id, description: "The activity item ID")
    field(:inserted_at, :datetime, description: "When was the activity inserted")
    field(:priority, :integer)
    field(:type, :activity_type)
    field(:subject, :string)
    field(:subject_params, list_of(:activity_param_item))
    field(:message, :string)
    field(:message_params, list_of(:activity_param_item))
    field(:object, :activity_object)
    field(:author, :actor)
    field(:group, :group)
  end
end
