
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace EonaCat.LogStack.Logging;

/// <summary>
/// DI integration for every .NET host type.
/// </summary>
public static class EonaCatLoggingExtensions
{
    public static ILoggingBuilder AddEonaCatLogStack(
        this ILoggingBuilder builder,
        Action<EonaCatLogStack>? configure = null)
    {
        var stack = new EonaCatLogStack();
        configure?.Invoke(stack);

        builder.Services.AddSingleton(stack);
        builder.Services.AddSingleton<ILoggerProvider>(
            new EonaCatLoggerProvider(stack));

        return builder;
    }

    public static IServiceCollection AddEonaCatLogStack(
        this IServiceCollection services,
        Action<EonaCatLogStack>? configure = null)
    {
        var stack = new EonaCatLogStack();
        configure?.Invoke(stack);

        services.AddSingleton(stack);
        services.AddSingleton<ILoggerProvider>(
            new EonaCatLoggerProvider(stack));

        services.AddSingleton<ILoggerFactory, LoggerFactory>();
        return services;
    }
}
