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

module Gql::Mutations
  class AdminPasswordAuthSend < BaseMutation
    include Gql::Concerns::HandlesThrottling

    description 'Sends an email with a token to login via password.'

    argument :login, String, 'Login information that is used to create a token.'

    field :success, Boolean, null: true, description: 'This indicates if sending the token was successful.'

    allow_public_access!

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

    def resolve(login:)
      begin
        Service::Auth::SendAdminToken.execute(login: login)
      rescue Service::Auth::SendAdminToken::TokenError, Service::Auth::SendAdminToken::EmailError
        return { success: false }
      end

      { success: true }
    end
  end
end
