// SPDX-License-Identifier: GPL-3.0-or-later

#include "../libnetdata.h"
#undef uuid_generate
#undef uuid_generate_random
#undef uuid_generate_time

#ifdef OS_WINDOWS
void os_uuid_generate(nd_uuid_t out) {
    // nd_uuid_t is byte-aligned storage, so use aligned local UUID storage
    // before copying the generated bytes to the caller's output buffer.
    UUID uuid;
    RPC_STATUS status = UuidCreate(&uuid);
    while (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) {
        tinysleep();
        status = UuidCreate(&uuid);
    }
    memcpy(out, &uuid, sizeof(uuid));
}

void os_uuid_generate_random(nd_uuid_t out) {
    os_uuid_generate(out);
}

void os_uuid_generate_time(nd_uuid_t out) {
    os_uuid_generate(out);
}

#else

#if defined(OS_MACOS)
#include <uuid/uuid.h>
#else
#include <uuid.h>
#endif

void os_uuid_generate(nd_uuid_t out) {
    // IMPORTANT: this generates a UUIDv4, which is random
    // and falls back to uuid_generate_time() if high resolution random generated is not available
    uuid_generate(out);
}

void os_uuid_generate_random(nd_uuid_t out) {
    uuid_generate_random(out);
}

void os_uuid_generate_time(nd_uuid_t out) {
    // IMPORTANT: this generates a UUIDv1, which is not random and may suffer from collisions
    uuid_generate_time(out);
}

#endif
