namespace ZB.MOM.WW.MxGateway.Server.Workers;

public sealed class WorkerClientConnection
{
    /// <summary>Initializes a new worker client connection.</summary>
    /// <param name="sessionId">Identifier of the session.</param>
    /// <param name="nonce">Worker handshake nonce.</param>
    /// <param name="stream">Named pipe stream for IPC communication.</param>
    /// <param name="frameOptions">Frame protocol serialization options.</param>
    /// <param name="processHandle">Worker process handle, if available.</param>
    public WorkerClientConnection(
        string sessionId,
        string nonce,
        Stream stream,
        WorkerFrameProtocolOptions frameOptions,
        WorkerProcessHandle? processHandle = null)
    {
        if (string.IsNullOrWhiteSpace(sessionId))
        {
            throw new ArgumentException("Session id is required.", nameof(sessionId));
        }

        if (string.IsNullOrWhiteSpace(nonce))
        {
            throw new ArgumentException("Worker nonce is required.", nameof(nonce));
        }

        SessionId = sessionId;
        Nonce = nonce;
        Stream = stream ?? throw new ArgumentNullException(nameof(stream));
        FrameOptions = frameOptions ?? throw new ArgumentNullException(nameof(frameOptions));
        ProcessHandle = processHandle;
    }

    /// <summary>The session ID associated with this connection.</summary>
    public string SessionId { get; }

    /// <summary>The nonce used for handshaking with the worker.</summary>
    public string Nonce { get; }

    /// <summary>The named pipe stream for IPC communication.</summary>
    public Stream Stream { get; }

    /// <summary>The frame protocol options for serialization.</summary>
    public WorkerFrameProtocolOptions FrameOptions { get; }

    /// <summary>The worker process handle, if available.</summary>
    public WorkerProcessHandle? ProcessHandle { get; }
}
