# frozen_string_literal: true

class Types::Account < Types::BaseObject
  implements Types::Interface::WithTimestamps

  description 'A user account on Kitsu'

  key fields: %w[id]

  def self.resolve_reference(reference, context)
    # Only resolve the user if the ID matches the current user
    return nil if context[:user].nil? || context[:user].id.to_s != reference[:id]

    context[:user]
  end

  field :id, ID, null: false

  field :email, [String],
    null: false,
    description: 'The email addresses associated with this account'

  # TODO: allow for multiple emails per user in the actual database
  def email
    [object.email]
  end

  field :profile, Types::Profile,
    null: false,
    description: 'The profile for this account'

  def profile
    object
  end

  field :pro_subscription, Types::ProSubscription,
    null: true,
    description: 'The PRO subscription for this account'

  field :max_pro_streak, Integer,
    null: true,
    description: 'Longest period an account has had a PRO subscription for in seconds'

  field :facebook_id, String,
    null: true,
    description: 'Facebook account linked to the account'

  field :twitter_id, String,
    null: true,
    description: 'Twitter account linked to the account'

  field :sfw_filter, Boolean,
    null: true,
    description: 'Whether Not Safe For Work content is accessible'

  field :sfw_filter_preference, Types::Enum::SfwFilterPreference,
    null: true,
    description: 'The level of the SFW Filter'

  field :language, String,
    null: true,
    description: 'Primary language for the account'

  field :title_language_preference, Types::Enum::TitleLanguagePreference,
    null: true,
    description: 'Preferred language for media titles'

  field :rating_system, Types::Enum::RatingSystem,
    null: false,
    description: 'Media rating system used for the account'

  field :time_zone, String,
    null: true,
    description: 'Time zone of the account'

  field :country, String,
    null: true,
    description: 'The country this user resides in'

  field :site_permissions, [Types::Enum::SitePermission],
    null: false,
    method: :permissions,
    description: 'The site-wide permissions this user has access to'

  field :enabled_features, [String],
    null: false,
    description: 'The features this user has access to'
end
