using System.Text.Json.Serialization;
namespace Mbproxy.Admin;
// ── Wire DTOs for GET /status.json ───────────────────────────────────────────
// Field names must match docs/Operations/StatusPage.md tables EXACTLY (camelCase via
// JsonKnownNamingPolicy.CamelCase on the source-gen context).
///
/// Top-level response envelope for GET /status.json.
///
public sealed record StatusResponse(
ServiceFields Service,
ListenersAggregate Listeners,
IReadOnlyList Plcs);
/// Service-wide identity and reload counters.
public sealed record ServiceFields(
long UptimeSeconds,
string Version,
DateTimeOffset? ConfigLastReloadUtc,
int ConfigReloadCount,
int ConfigReloadRejectedCount);
/// Aggregate listener state across all configured PLCs.
public sealed record ListenersAggregate(int Bound, int Configured);
/// Per-PLC status row.
public sealed record PlcStatus(
string Name,
string Host,
int ListenPort,
PlcListenerStatus Listener,
PlcClientsStatus Clients,
PlcPdusStatus Pdus,
PlcBackendStatus Backend,
PlcBytesStatus Bytes);
/// Listener state sub-object.
public sealed record PlcListenerStatus(
string State,
string? LastBindError,
int RecoveryAttempts);
/// Connected-clients sub-object.
public sealed record PlcClientsStatus(
int Connected,
IReadOnlyList RemoteEndpoints);
/// Per-connection-pair snapshot for the status page.
public sealed record ClientSnapshot(
string Remote,
DateTimeOffset ConnectedAtUtc,
long PdusForwarded);
/// PDU counters sub-object.
public sealed record PlcPdusStatus(
long Forwarded,
FcCounts ByFc,
long RewrittenSlots,
long PartialBcdWarnings,
///
/// Count of BCD-rewriter slot decisions where the wire value was not a valid BCD
/// nibble pattern (e.g. 0xABCD at a tag address). The slot passes through
/// unrewritten and this counter increments.
///
long InvalidBcdWarnings);
/// Per-function-code request counts.
public sealed record FcCounts(
long Fc03,
long Fc04,
long Fc06,
long Fc16,
long Other);
///
/// Backend connect, exception, and multiplexer telemetry, including the in-flight
/// multiplexer fields (InFlight, MaxInFlight, TxIdWraps,
/// DisconnectCascades, QueueDepth), the read-coalescing counters
/// (CoalescedHitCount, CoalescedMissCount, CoalescedResponseToDeadUpstream),
/// and the response-cache counters (CacheHitCount, CacheMissCount,
/// CacheInvalidations, CacheEntryCount, CacheBytes).
///
/// The dashboard-side derived ratios coalescingRatio and cacheHitRatio
/// are intentionally NOT carried on the wire — consumers compute Hit / (Hit + Miss)
/// from the raw counters.
///
public sealed record PlcBackendStatus(
long ConnectsSuccess,
long ConnectsFailed,
ExceptionCounts ExceptionsByCode,
double LastRoundTripMs,
long InFlight,
long MaxInFlight,
long TxIdWraps,
long DisconnectCascades,
long QueueDepth,
long CoalescedHitCount,
long CoalescedMissCount,
long CoalescedResponseToDeadUpstream,
long CacheHitCount,
long CacheMissCount,
long CacheInvalidations,
long CacheEntryCount,
long CacheBytes,
/// Backend keepalive heartbeat probes issued on idle backend sockets.
long BackendHeartbeatsSent,
/// Keepalive heartbeat probes that timed out (backend not answering).
long BackendHeartbeatsFailed,
/// Backend teardowns triggered by a failed keepalive heartbeat.
long BackendIdleDisconnects);
/// Modbus exception counts by code.
public sealed record ExceptionCounts(
long Code01,
long Code02,
long Code03,
long Code04,
///
/// Backend exceptions whose response code is not 01–04 (e.g. 0x06 Server Device
/// Busy, 0x0B Gateway Target Failed To Respond, vendor-specific codes).
///
long CodeOther);
/// Byte-transfer counters.
public sealed record PlcBytesStatus(
long UpstreamIn,
long UpstreamOut);
// ── Source-generation context ─────────────────────────────────────────────────
// TreatWarningsAsErrors is on, so the context must include every reachable type.
[JsonSerializable(typeof(StatusResponse))]
[JsonSourceGenerationOptions(
WriteIndented = false,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
internal partial class StatusJsonContext : JsonSerializerContext;