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

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

    field :success, Boolean, null: false, description: 'Was the reset successful?'
    field :overviews, [Gql::Types::OverviewType], null: true, description: 'List of overview sortings for the user'

    requires_permission 'user_preferences.overview_sorting'

    def resolve
      ActiveRecord::Base.transaction do
        ::User::OverviewSorting.where(user: context.current_user).destroy_all
      end

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

      {
        success:   true,
        overviews: Service::User::Overview::List.with_current_user(context.current_user).execute(ignore_user_conditions: true)
      }
    end
  end
end
