#include "credentials.hpp"
#include "../logger/type.hpp"

namespace fz::authentication {

bool credentials::verify(const any_method &method, impersonation_context &impersonation, logger_interface &logger) const
{
	if (auto m = method.is<method::password>()) {
		return password.verify(m->data, impersonation, logger);
	}

	if (auto m = method.is<method::token>()) {
		return m->manager.get().verify(m->data, impersonation);
	}

	return false;
}

bool credentials::is_valid_for(const available_methods &m, logger_interface &l) const
{
	auto warn = [&](auto &&... args) {
		l.log_u(logmsg::warning, std::forward<decltype(args)>(args)...);
		return false;
	};

	if (m.is_auth_necessary()) {
		if (m.can_verify(method::password()) && !password) {
			return warn(L"Auth method 'password' is required but no password is available");
		}
	}

	return true;
}

available_methods credentials::get_most_secure_methods() const
{
	methods_set ms;

	if (password) {
		ms.add<method::password>();
	}

	return { ms };
}

}
