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

namespace ZB.MOM.WW.MxGateway.Client;

/// <summary>Exception thrown when an MXAccess command fails with a non-zero HResult or failing status.</summary>
public sealed class MxAccessException : MxGatewayCommandException
{
    /// <summary>Initializes a new instance with the given message, reply, and optional inner exception.</summary>
    /// <param name="message">The error message describing the MXAccess failure.</param>
    /// <param name="reply">The MxCommandReply containing the failure details (statuses, HResult, etc.).</param>
    /// <param name="innerException">The underlying exception, if any.</param>
    public MxAccessException(
        string message,
        MxCommandReply reply,
        Exception? innerException = null)
        : base(
            message,
            reply.SessionId,
            reply.CorrelationId,
            reply.ProtocolStatus,
            reply.HasHresult ? reply.Hresult : null,
            reply.Statuses.ToArray(),
            innerException)
    {
        Reply = reply;
    }

    /// <summary>Gets the underlying MxCommandReply containing full failure details.</summary>
    public MxCommandReply Reply { get; }
}
