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

module Gql::Mutations
  class User::Current::PasswordCheck < BaseMutation
    include Gql::Concerns::HandlesThrottling

    description 'Check your password'

    argument :password, String, required: true, description: 'Password to check'

    field :success, Boolean, description: 'This indicates if given password matches current user password'
    field :token, String, description: 'One-time token which should be included in a subsequent request (where applicable)' # rubocop:disable Zammad/GraphqlForbidSensitiveFields -- Single-use confirmation token returned to the calling user; not a long-lived secret.

    requires_permission 'user_preferences.password'

    def throttle_if_needed!(...)
      throttle!(limit: 10, period: 1.minute, by_identifier: context.current_user.login)
    end

    def resolve(password:)
      password_check = Service::User::PasswordCheck
        .with_current_user(context.current_user)
        .execute(password:)

      if !password_check[:success]
        return error_response({ field: :password, message: __('The provided password is incorrect.') })
      end

      password_check
    end
  end
end
