using ZB.MOM.WW.MxGateway.Contracts.Proto;

namespace ZB.MOM.WW.MxGateway.Client;

/// <summary>Extension methods for MxStatusProxy values.</summary>
public static class MxStatusProxyExtensions
{
    /// <summary>
    /// Returns whether the status indicates success, which the wire contract defines as
    /// <see cref="MxStatusCategory.Ok"/>. The raw <c>Success</c> member is a verbatim COM
    /// diagnostic, not a boolean, so it never participates in the verdict.
    /// </summary>
    /// <param name="status">The status to check.</param>
    /// <returns><see langword="true"/> if the status indicates success; otherwise <see langword="false"/>.</returns>
    public static bool IsSuccess(this MxStatusProxy status)
    {
        ArgumentNullException.ThrowIfNull(status);

        return status.Category is MxStatusCategory.Ok;
    }

    /// <summary>Returns a formatted summary of the status for diagnostic output.</summary>
    /// <param name="status">The status to summarize.</param>
    /// <returns>A formatted diagnostic summary string.</returns>
    public static string ToDiagnosticSummary(this MxStatusProxy status)
    {
        ArgumentNullException.ThrowIfNull(status);

        string diagnosticText = string.IsNullOrWhiteSpace(status.DiagnosticText)
            ? "no diagnostic text"
            : status.DiagnosticText;

        return $"success={status.Success}; {status.Category} by {status.DetectedBy}; detail={status.Detail}; {diagnosticText}";
    }
}
