using System.Security.Cryptography;
using System.Text;
namespace AIStudio.Assistants.BeyondFactsLab;
///
/// Derives the ID under which the BeyondFacts Lab stores the research on one person from the name of the person.
///
///
/// The ID follows from the name alone, see BflParticipant.NameKey, so that the research on a person is found again
/// after the list of participants was researched anew, and no path is stored. It is a UUID of version 8 built from
/// SHA-256, like BflThreadId.
///
public static class BflPersonId
{
///
/// What comes before every name, so that these IDs never meet those of other kinds.
///
private const string NAMESPACE = "beyondfacts-lab/person/";
///
/// Gets the ID of a person.
///
/// The name of the person; case and the spaces between its words do not count.
public static Guid Of(string name)
{
var bytesOfName = Encoding.UTF8.GetBytes($"{NAMESPACE}{BflParticipant.NameKey(name)}");
Span hash = stackalloc byte[SHA256.HashSizeInBytes];
SHA256.HashData(bytesOfName, hash);
var bytes = hash[..16];
bytes[6] = (byte)((bytes[6] & 0x0F) | 0x80);
bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80);
return new Guid(bytes, bigEndian: true);
}
}