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

class Service::User::PasswordReset::Update < Service::Base

  attr_reader :token, :password

  def initialize(token:, password:)
    @token = token
    @password = password
  end

  def execute
    Service::CheckFeatureEnabled.execute(name: 'user_lost_password')

    PasswordPolicy.new(password).valid!

    user = ::User.password_reset_via_token(token, password)
    raise InvalidTokenError if !user
    raise EmailError if user.email.blank?

    Service::User::SendPasswordChangeNotification.execute(user: user)

    user
  end

  class InvalidTokenError < StandardError
    def initialize
      super(__('The provided token is invalid.'))
    end
  end

  class EmailError < StandardError
    def initialize
      super(__('The email could not be sent to the user.'))
    end
  end
end
