namespace ZB.MOM.WW.OtOpcUa.Commons.Types;

public readonly record struct CorrelationId(Guid Value)
{
    /// <summary>Creates a new CorrelationId with a randomly generated GUID.</summary>
    /// <returns>A new, randomly generated <see cref="CorrelationId"/>.</returns>
    public static CorrelationId NewId() => new(Guid.NewGuid());
    /// <inheritdoc />
    public override string ToString() => Value.ToString("N");
    /// <summary>Parses a lowercase hex string without hyphens into a CorrelationId.</summary>
    /// <param name="s">The string to parse.</param>
    /// <returns>The parsed <see cref="CorrelationId"/>.</returns>
    public static CorrelationId Parse(string s) => new(Guid.ParseExact(s, "N"));
    /// <summary>Attempts to parse a lowercase hex string without hyphens into a CorrelationId.</summary>
    /// <param name="s">The string to parse, or null.</param>
    /// <param name="id">The resulting CorrelationId if parsing succeeds.</param>
    /// <returns><c>true</c> if <paramref name="s"/> was successfully parsed.</returns>
    public static bool TryParse(string? s, out CorrelationId id)
    {
        if (Guid.TryParseExact(s, "N", out var g)) { id = new CorrelationId(g); return true; }
        id = default; return false;
    }
}
