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

public sealed record GatewayLogScope(
    string? SessionId = null,
    int? WorkerProcessId = null,
    ulong? CorrelationId = null,
    string? CommandMethod = null,
    string? ClientIdentity = null)
{
    /// <summary>Converts the log scope to a dictionary with redacted sensitive fields.</summary>
    /// <returns>A dictionary containing only the scope's present fields, with sensitive values redacted.</returns>
    public IReadOnlyDictionary<string, object?> ToDictionary()
    {
        Dictionary<string, object?> values = [];

        AddIfPresent(values, "SessionId", SessionId);
        AddIfPresent(values, "WorkerProcessId", WorkerProcessId);
        AddIfPresent(values, "CorrelationId", CorrelationId);
        AddIfPresent(values, "CommandMethod", CommandMethod);
        AddIfPresent(values, "ClientIdentity", GatewayLogRedactor.RedactClientIdentity(ClientIdentity));

        return values;
    }

    private static void AddIfPresent(
        Dictionary<string, object?> values,
        string key,
        object? value)
    {
        if (value is not null)
        {
            values[key] = value;
        }
    }
}
