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

public readonly record struct ExecutionId(Guid Value)
{
    /// <summary>Creates a new execution ID with a randomly generated GUID.</summary>
    /// <returns>A new ExecutionId instance.</returns>
    public static ExecutionId NewId() => new(Guid.NewGuid());

    /// <inheritdoc />
    public override string ToString() => Value.ToString("N");

    /// <summary>Parses the specified string into an ExecutionId in format N.</summary>
    /// <param name="s">The string to parse.</param>
    /// <returns>The parsed ExecutionId.</returns>
    public static ExecutionId Parse(string s) => new(Guid.ParseExact(s, "N"));

    /// <summary>Tries to parse the specified string into an ExecutionId in format N.</summary>
    /// <param name="s">The string to parse, or null.</param>
    /// <param name="id">The parsed ExecutionId, or default if parsing fails.</param>
    /// <returns>true if parsing succeeded; otherwise, false.</returns>
    public static bool TryParse(string? s, out ExecutionId id)
    {
        if (Guid.TryParseExact(s, "N", out var g)) { id = new ExecutionId(g); return true; }
        id = default; return false;
    }
}
