#include <koalabox/logger.hpp>

#include "smoke_api/interfaces/steam_user.hpp"
#include "smoke_api/config.hpp"

namespace smoke_api::steam_user {
    EUserHasLicenseForAppResult UserHasLicenseForApp(
        const std::string& function_name,
        const AppId_t appId,
        const AppId_t dlcId,
        const std::function<EUserHasLicenseForAppResult()>& original_function
    ) noexcept {
        try {
            const auto result = original_function();

            if(result == k_EUserHasLicenseResultNoAuth) {
                LOG_WARN("{} -> App ID: {:>8}, Result: NoAuth", function_name, dlcId);
                return result;
            }

            const auto has_license = config::is_dlc_unlocked(
                appId,
                dlcId,
                [&] {
                    return result == k_EUserHasLicenseResultHasLicense;
                }
            );

            LOG_INFO("{} -> App ID: {:>8}, HasLicense: {}", function_name, dlcId, has_license);

            return has_license
                       ? k_EUserHasLicenseResultHasLicense
                       : k_EUserHasLicenseResultDoesNotHaveLicense;
        } catch(const std::exception& e) {
            LOG_ERROR("{} -> Error: {}", function_name, e.what());
            return k_EUserHasLicenseResultDoesNotHaveLicense;
        }
    }
}
