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

public readonly record struct DeploymentId(Guid Value)
{
    /// <summary>Creates a new deployment ID with a random GUID.</summary>
    /// <returns>A new DeploymentId.</returns>
    public static DeploymentId NewId() => new(Guid.NewGuid());

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

    /// <summary>Parses a deployment ID from a hex string without hyphens.</summary>
    /// <param name="s">The hex string to parse.</param>
    /// <returns>The parsed DeploymentId.</returns>
    public static DeploymentId Parse(string s) => new(Guid.ParseExact(s, "N"));

    /// <summary>Attempts to parse a deployment ID from a hex string without hyphens.</summary>
    /// <param name="s">The hex string to parse, or null.</param>
    /// <param name="id">The parsed DeploymentId if successful, or default.</param>
    /// <returns>True if parsing succeeded; false otherwise.</returns>
    public static bool TryParse(string? s, out DeploymentId id)
    {
        if (Guid.TryParseExact(s, "N", out var g)) { id = new DeploymentId(g); return true; }
        id = default; return false;
    }
}
