const std = @import("std");
const std_compat = @import("compat.zig");

/// Generate a UUID v4 string (36 chars: 8-4-4-4-12)
pub fn generateId() [36]u8 {
    var bytes: [16]u8 = undefined;
    std_compat.crypto.random.bytes(&bytes);

    // Set version 4
    bytes[6] = (bytes[6] & 0x0f) | 0x40;
    // Set variant 1
    bytes[8] = (bytes[8] & 0x3f) | 0x80;

    var buf: [36]u8 = undefined;
    const hex = "0123456789abcdef";
    var out: usize = 0;
    for (bytes, 0..) |b, i| {
        if (i == 4 or i == 6 or i == 8 or i == 10) {
            buf[out] = '-';
            out += 1;
        }
        buf[out] = hex[b >> 4];
        buf[out + 1] = hex[b & 0x0f];
        out += 2;
    }
    return buf;
}

/// Current time in milliseconds since epoch
pub fn nowMs() i64 {
    return std_compat.time.milliTimestamp();
}
