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

namespace ZB.MOM.WW.MxGateway.Client;

/// <summary>
/// Exception thrown when a gateway RPC call fails or returns an error status.
/// </summary>
public class MxGatewayException : Exception
{
    /// <summary>
    /// Initializes a new instance of the MxGatewayException class with the specified message.
    /// </summary>
    /// <param name="message">Diagnostic message describing the failure.</param>
    public MxGatewayException(string message)
        : base(message)
    {
        Statuses = [];
    }

    /// <summary>
    /// Initializes a new instance of the MxGatewayException class with the specified message and inner exception.
    /// </summary>
    /// <param name="message">Diagnostic message describing the failure.</param>
    /// <param name="innerException">Underlying exception that caused this failure.</param>
    public MxGatewayException(string message, Exception? innerException)
        : base(message, innerException)
    {
        Statuses = [];
    }

    /// <summary>
    /// Initializes a new instance of the MxGatewayException class with full diagnostic information.
    /// </summary>
    /// <param name="message">Diagnostic message describing the failure.</param>
    /// <param name="sessionId">Session ID associated with the exception, if available.</param>
    /// <param name="correlationId">Correlation ID associated with the exception, if available.</param>
    /// <param name="protocolStatus">Protocol-level status returned by the gateway, if available.</param>
    /// <param name="hResult">HRESULT code returned by the worker or MXAccess, if available.</param>
    /// <param name="statuses">List of MXAccess status codes returned by the operation.</param>
    /// <param name="innerException">Underlying exception that caused this failure.</param>
    public MxGatewayException(
        string message,
        string? sessionId,
        string? correlationId,
        ProtocolStatus? protocolStatus,
        int? hResult,
        IReadOnlyList<MxStatusProxy> statuses,
        Exception? innerException = null)
        : base(message, innerException)
    {
        SessionId = sessionId;
        CorrelationId = correlationId;
        ProtocolStatus = protocolStatus;
        HResultCode = hResult;
        Statuses = statuses;
    }

    /// <summary>
    /// Gets the session ID associated with the exception, if available.
    /// </summary>
    public string? SessionId { get; }

    /// <summary>
    /// Gets the correlation ID associated with the exception, if available.
    /// </summary>
    public string? CorrelationId { get; }

    /// <summary>
    /// Gets the protocol-level status returned by the gateway, if available.
    /// </summary>
    public ProtocolStatus? ProtocolStatus { get; }

    /// <summary>
    /// Gets the HRESULT code returned by the worker or MXAccess, if available.
    /// </summary>
    public int? HResultCode { get; }

    /// <summary>
    /// Gets the list of MXAccess status codes returned by the operation.
    /// </summary>
    public IReadOnlyList<MxStatusProxy> Statuses { get; }
}
