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

/// <summary>
/// SHA-256 hex digest identifying a config snapshot revision. Storage form is lowercase
/// 64-char hex (no <c>0x</c> prefix). Empty hash is invalid.
/// </summary>
public readonly record struct RevisionHash(string Value)
{
    /// <inheritdoc />
    public override string ToString() => Value;
    /// <summary>
    /// Parses a string into a <see cref="RevisionHash"/>.
    /// </summary>
    /// <param name="s">The string to parse.</param>
    /// <returns>A <see cref="RevisionHash"/> instance.</returns>
    /// <exception cref="ArgumentException">Thrown if the input is null, empty, or whitespace.</exception>
    public static RevisionHash Parse(string s) =>
        string.IsNullOrWhiteSpace(s)
            ? throw new ArgumentException("RevisionHash value cannot be empty.", nameof(s))
            : new RevisionHash(s);
    /// <summary>
    /// Attempts to parse a string into a <see cref="RevisionHash"/>.
    /// </summary>
    /// <param name="s">The string to parse.</param>
    /// <param name="hash">The parsed hash, or default if parsing fails.</param>
    /// <returns>True if parsing succeeded; otherwise false.</returns>
    public static bool TryParse(string? s, out RevisionHash hash)
    {
        if (!string.IsNullOrWhiteSpace(s)) { hash = new RevisionHash(s); return true; }
        hash = default; return false;
    }
}
