using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using ZB.MOM.WW.Telemetry;

namespace ZB.MOM.WW.Telemetry.Serilog;

/// <summary>
/// Extension point for configuring the shared Serilog application logger on an
/// <see cref="IHostApplicationBuilder"/>. Wires config-driven sinks
/// (<c>ReadFrom.Configuration</c>), an explicit minimum level (<c>Serilog:MinimumLevel</c>,
/// default <see cref="LogEventLevel.Information"/>), and the shared enricher/redaction/OTel-export
/// set via <see cref="ZbSerilogConfig"/>. Does NOT configure OTel metrics/traces — call
/// <c>AddZbTelemetry</c> in the core package for that.
///
/// <para>
/// This method intentionally does <strong>not</strong> set the process-global
/// <see cref="Log.Logger"/> (via <c>CreateBootstrapLogger</c> or otherwise). Mutating
/// process-global state in a shared library causes "logger is already frozen" exceptions
/// when multiple hosts are built in the same process (integration tests, multi-host apps).
/// </para>
/// <para>
/// Apps that need a pre-<c>Build()</c> bootstrap logger to capture early startup exceptions
/// should set <see cref="Log.Logger"/> themselves in <c>Program.cs</c> before calling
/// <c>AddZbSerilog</c>:
/// <code>
/// Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateBootstrapLogger();
/// // ... then build the host ...
/// builder.AddZbSerilog(o => { ... });
/// </code>
/// This keeps global-state mutation firmly in the application, not the library.
/// </para>
/// </summary>
public static class ZbSerilogExtensions
{
    /// <summary>
    /// Registers the Serilog application logger in DI. Wires configuration-driven sinks
    /// (<c>ReadFrom.Configuration</c>), a code default of <see cref="LogEventLevel.Information"/>
    /// that config can override via <c>Serilog:MinimumLevel</c> or namespace overrides, plus
    /// the identity enrichers (<c>SiteId</c>/<c>NodeRole</c> from <paramref name="configure"/>,
    /// <c>NodeHostname</c> = <see cref="System.Environment.MachineName"/>).
    ///
    /// <para>
    /// This method does <strong>not</strong> set the process-global <see cref="Log.Logger"/>.
    /// <c>preserveStaticLogger: true</c> is passed to <c>AddSerilog</c> so the static logger
    /// is left entirely untouched — safe to call multiple times in the same process (integration
    /// tests, multi-host scenarios) without hitting "logger is already frozen".
    /// </para>
    /// <para>
    /// If early-startup bootstrap logging is required (before <c>Build()</c>), set
    /// <c>Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateBootstrapLogger();</c>
    /// in <c>Program.cs</c> before calling this method. That decision belongs to the
    /// application, not the shared library.
    /// </para>
    /// </summary>
    /// <param name="builder">The host application builder.</param>
    /// <param name="configure">Populates the <see cref="ZbTelemetryOptions"/>.</param>
    public static IHostApplicationBuilder AddZbSerilog(
        this IHostApplicationBuilder builder,
        Action<ZbTelemetryOptions> configure)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentNullException.ThrowIfNull(configure);

        var options = new ZbTelemetryOptions();
        configure(options);

        // Fail fast on a malformed OTLP endpoint with a clear, named message — same validation the
        // core AddZbTelemetry path uses — instead of a late error when the OTel log sink builds.
        ZbTelemetryOptionsValidator.Validate(options, nameof(configure));

        // Register the application logger in DI only. preserveStaticLogger: true ensures
        // AddSerilog does NOT freeze or replace Log.Logger — critical for multi-host
        // processes (integration tests etc.) where AddZbSerilog may be called more than once.
        builder.Services.AddSerilog(
            (serviceProvider, loggerConfiguration) =>
            {
                loggerConfiguration
                    .MinimumLevel.Is(LogEventLevel.Information)
                    .ReadFrom.Configuration(builder.Configuration);

                ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider);
            },
            preserveStaticLogger: true);

        return builder;
    }
}
