using System;

namespace ZB.MOM.WW.MxGateway.Worker.MxAccess;

/// <summary>
/// Captures details about an MXAccess operation that failed during shutdown.
/// </summary>
public sealed class MxAccessShutdownFailure
{
    /// <summary>
    /// Initializes the shutdown failure record.
    /// </summary>
    /// <param name="operation">Name of the operation that failed.</param>
    /// <param name="serverHandle">Server handle if applicable.</param>
    /// <param name="itemHandle">Item handle if applicable.</param>
    /// <param name="exception">Exception that was raised.</param>
    public MxAccessShutdownFailure(
        string operation,
        int? serverHandle,
        int? itemHandle,
        Exception exception)
    {
        if (string.IsNullOrWhiteSpace(operation))
        {
            throw new ArgumentException("Shutdown failure operation is required.", nameof(operation));
        }

        Operation = operation;
        ServerHandle = serverHandle;
        ItemHandle = itemHandle;
        ExceptionType = exception?.GetType().FullName ?? string.Empty;
        HResult = exception?.HResult;
    }

    /// <summary>
    /// The operation that failed (e.g., Unregister, RemoveItem).
    /// </summary>
    public string Operation { get; }

    /// <summary>
    /// Server handle if applicable; otherwise null.
    /// </summary>
    public int? ServerHandle { get; }

    /// <summary>
    /// Item handle if applicable; otherwise null.
    /// </summary>
    public int? ItemHandle { get; }

    /// <summary>
    /// Full type name of the exception, or empty string if no exception.
    /// </summary>
    public string ExceptionType { get; }

    /// <summary>
    /// HResult code if the exception has one; otherwise null.
    /// </summary>
    public int? HResult { get; }
}
