namespace Mbproxy.Proxy.Multiplexing;

/// <summary>
/// Source-generated <see cref="LoggerMessage"/> definitions for the TxId-multiplexing
/// connection layer. Event names are stable — do not rename without updating
/// docs/Reference/LogEvents.md's "Logging" event-name table.
/// </summary>
internal static partial class MultiplexerLogEvents
{
    /// <summary>
    /// Emitted once per upstream client accept.
    /// </summary>
    [LoggerMessage(
        EventId   = 110,
        EventName = "mbproxy.client.connected",
        Level     = LogLevel.Information,
        Message   = "Client connected: Plc={Plc} RemoteEp={RemoteEp}")]
    public static partial void ClientConnected(
        ILogger logger,
        string  plc,
        string  remoteEp);

    /// <summary>
    /// Emitted when an upstream pipe is closed (clean disconnect, fault, or cascade).
    /// </summary>
    [LoggerMessage(
        EventId   = 111,
        EventName = "mbproxy.client.disconnected",
        Level     = LogLevel.Information,
        Message   = "Client disconnected: Plc={Plc} RemoteEp={RemoteEp} Reason={Reason}")]
    public static partial void ClientDisconnected(
        ILogger logger,
        string  plc,
        string  remoteEp,
        string  reason);

    /// <summary>
    /// Emitted when the multiplexer successfully opens its single backend connection to a PLC.
    /// </summary>
    [LoggerMessage(
        EventId   = 112,
        EventName = "mbproxy.multiplex.backend.connected",
        Level     = LogLevel.Information,
        Message   = "Backend multiplex connection up: Plc={Plc} Host={Host} Port={Port}")]
    public static partial void BackendConnected(
        ILogger logger,
        string  plc,
        string  host,
        int     port);

    /// <summary>
    /// Emitted when the multiplexer cascades a backend disconnect to all attached upstream
    /// clients. <c>UpstreamCount</c> is the number of upstream pipes that were closed and
    /// <c>InFlightCount</c> is the number of in-flight requests dropped.
    /// </summary>
    [LoggerMessage(
        EventId   = 113,
        EventName = "mbproxy.multiplex.backend.disconnected",
        Level     = LogLevel.Warning,
        Message   = "Backend multiplex connection down: Plc={Plc} UpstreamCount={UpstreamCount} InFlightCount={InFlightCount} Reason={Reason}")]
    public static partial void BackendDisconnected(
        ILogger logger,
        string  plc,
        int     upstreamCount,
        int     inFlightCount,
        string  reason);

    /// <summary>
    /// Emitted once when the TxId allocator refuses to allocate — every slot in the 16-bit
    /// space is currently in flight. The multiplexer responds to the upstream with a
    /// Modbus exception (code 04 / Slave Device Failure). Realistically unreachable under
    /// normal load (ECOM serializes at ~2-10 ms per request); a stress-only path.
    /// </summary>
    [LoggerMessage(
        EventId   = 114,
        EventName = "mbproxy.multiplex.saturated",
        Level     = LogLevel.Error,
        Message   = "Multiplexer TxId space saturated — returning exception 04 to upstream: Plc={Plc} RemoteEp={RemoteEp}")]
    public static partial void Saturated(
        ILogger logger,
        string  plc,
        string  remoteEp);

    /// <summary>
    /// Emitted when the backend connect Polly pipeline fails.
    /// </summary>
    [LoggerMessage(
        EventId   = 115,
        EventName = "mbproxy.backend.failed",
        Level     = LogLevel.Warning,
        Message   = "Backend connect failed: Plc={Plc} Reason={Reason}")]
    public static partial void BackendFailed(
        ILogger logger,
        string  plc,
        string  reason);

    /// <summary>
    /// Emitted when the per-request watchdog times out an in-flight request whose response
    /// never arrived within <c>BackendRequestTimeoutMs</c>. The upstream party receives a
    /// Modbus exception (code 0x0B / Gateway Target Device Failed To Respond) and the
    /// proxy TxId is freed. Causes include: PLC dropped the response, network packet loss,
    /// or a backend that echoes the wrong MBAP TxId (e.g. pymodbus 3.13.0's
    /// concurrent-multiplexed-request bug).
    /// </summary>
    [LoggerMessage(
        EventId   = 116,
        EventName = "mbproxy.multiplex.request.timeout",
        Level     = LogLevel.Warning,
        Message   = "In-flight request timed out: Plc={Plc} ProxyTxId={ProxyTxId} OriginalTxId={OriginalTxId} Fc={Fc} ElapsedMs={ElapsedMs}")]
    public static partial void RequestTimeout(
        ILogger logger,
        string  plc,
        ushort  proxyTxId,
        ushort  originalTxId,
        byte    fc,
        long    elapsedMs);
}
