const std = @import("std");

/// UUID v4 generator - generates random UUIDs
pub fn generateV4(allocator: std.mem.Allocator) ![]const u8 {
    var bytes: [16]u8 = undefined;
    std.crypto.random.bytes(&bytes);
    
    // Set version (4) and variant bits
    bytes[6] = (bytes[6] & 0x0F) | 0x40; // Version 4
    bytes[8] = (bytes[8] & 0x3F) | 0x80; // Variant 10
    
    // Format as string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    const uuid_str = try allocator.alloc(u8, 36);
    
    const hex_chars = "0123456789abcdef";
    
    var i: usize = 0;
    var j: usize = 0;
    while (i < 16) : (i += 1) {
        uuid_str[j] = hex_chars[bytes[i] >> 4];
        uuid_str[j + 1] = hex_chars[bytes[i] & 0x0F];
        j += 2;
        
        // Add dashes at positions 8, 12, 16, 20
        if (i == 3 or i == 5 or i == 7 or i == 9) {
            uuid_str[j] = '-';
            j += 1;
        }
    }
    
    return uuid_str;
}

/// Generate a simple random ID (shorter than UUID, for internal use)
pub fn generateSimpleID(allocator: std.mem.Allocator, length: usize) ![]const u8 {
    const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    const id = try allocator.alloc(u8, length);
    
    for (id) |*c| {
        c.* = chars[std.crypto.random.int(usize) % chars.len];
    }
    
    return id;
}
