#include "system_identity_provider.hpp"

namespace fz::authentication
{

system_identity_provider::system_identity_provider(logger_interface &logger)
	: logger_(logger, "System identity provider")
{
}

error system_identity_provider::get_user_info(std::string_view username, const parameters &, user_info &uinfo)
{
#ifdef FZ_WINDOWS
	if (username.find_first_of("@\\") != username.npos) {
		return error::user_nonexisting;
	}

	auto account = fz::to_native_from_utf8(".\\" + std::string(username));
#else
	auto account = fz::to_native_from_utf8(username);
#endif

	if (auto uid = fz::get_user_uid(account); !uid.empty()) {
		uinfo.uid = std::move(uid);
		uinfo.account = std::move(account);

		return error::none;
	}

	return error::user_nonexisting;
}

const std::string system_identity_provider::name = "system";

const std::string &system_identity_provider::get_name() const
{
	return name;
}

std::optional<identity_provider::user_properties> system_identity_provider::get_properties(const parameters &) const
{
	user_properties up;
	up.password = password::with_impersonation::impersonation();

	return up;
}

system_identity_provider::user_properties system_identity_provider::get_default_user_properties() const
{
	user_properties up;

	up.mount_table = { { "/", fzT("%<home>"), fz::tvfs::mount_point::read_write } };
	up.description = "This user can impersonate any system user.";
	up.methods = { method::password() };
	up.password = password::with_impersonation::impersonation();

	return up;
}

}
