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

namespace EonaCat.Logger.EonaCatCoreLogger.Extensions
{
    /// <summary>
    /// Provides extension methods for registering a batching database logger with an <see cref="ILoggingBuilder"/>.
    /// </summary>
    /// <remarks>These extension methods enable the integration of a batching database logger into the logging
    /// pipeline using dependency injection. Use these methods to configure and add the logger to your application's
    /// logging system.</remarks>
    public static class BatchingDatabaseLoggerFactoryExtensions
    {
        /// <summary>
        /// Adds a batching database logger to the logging builder, allowing log messages to be written to a database in
        /// batches.
        /// </summary>
        /// <remarks>Use this method to enable efficient, batched logging to a database. Batching can
        /// improve performance by reducing the number of database writes. Configure batching behavior and database
        /// connection settings using the provided options.</remarks>
        /// <param name="builder">The logging builder to which the batching database logger will be added.</param>
        /// <param name="configure">An action to configure the batching database logger options. Cannot be null.</param>
        /// <returns>The same instance of <see cref="ILoggingBuilder"/> for chaining.</returns>
        public static ILoggingBuilder AddEonaCatBatchingDatabaseLogger(this ILoggingBuilder builder, Action<BatchingDatabaseLoggerOptions> configure)
        {
            var options = new BatchingDatabaseLoggerOptions();
            configure?.Invoke(options);

            builder.Services.AddSingleton<ILoggerProvider>(new BatchingDatabaseLoggerProvider(options));
            return builder;
        }
    }
}
