# frozen_string_literal: true

module Api
  module V1
    class UserBlueprint < ApiBlueprint
      view :minimal do
        fields :username, :language, :private, :gender, :theme, :role, :granblue_id, :show_gamertag, :show_granblue_id
        # Return collection_privacy as integer (enum returns string by default)
        field :collection_privacy do |user|
          User.collection_privacies[user.collection_privacy]
        end
        field :avatar do |user|
          {
            picture: user.picture,
            element: user.element
          }
        end
        # Use preloaded active_crew_membership to avoid N+1
        field :gamertag, if: ->(_, user, _) {
          user.show_gamertag && user.active_crew_membership&.crew&.gamertag.present?
        } do |user|
          user.active_crew_membership.crew.gamertag
        end
      end

      view :profile do
        include_view :minimal

        field :parties, if: ->(_fn, _obj, options) { options[:parties].length.positive? } do |_, options|
          PartyBlueprint.render_as_hash(options[:parties], view: :preview)
        end
      end

      view :token do
        fields :username, :token
      end

      # Settings view includes all user data + email (only for authenticated user viewing own settings)
      view :settings do
        include_view :minimal
        fields :email
      end
    end
  end
end
