# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

module Gql::Mutations
  class User::Current::Overview::UpdateOrder < BaseMutation
    description 'Update the overview sorting for the current user'

    argument :overview_ids, [GraphQL::Types::ID], description: 'The ordered list of overviews' # , loads: Gql::Types::OverviewType

    field :success, Boolean, null: false, description: 'Was the reset successful?'

    requires_permission 'user_preferences.overview_sorting'

    def resolve(overview_ids:)
      Service::User::Overview::UpdateOrder
        .with_current_user(context.current_user)
        .execute(authorized_overviews(overview_ids))

      Gql::Subscriptions::User::Current::OverviewOrderingUpdates
        .trigger_by(context.current_user)

      { success: true }
    end

    private

    def authorized_overviews(overview_ids)
      overview_ids
        .filter_map { |elem| load(elem) }

    end

    def load(gql_id)
      Gql::ZammadSchema
        .authorized_object_from_id(gql_id, type: Overview, user: context.current_user, query: :use?)
    rescue Exceptions::Forbidden
      nil
    end
  end
end
