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

namespace EonaCat.Logger.EonaCatCoreLogger.Extensions
{
    /// <summary>
    /// Provides extension methods for registering the EonaCat database logger with an <see cref="ILoggingBuilder"/>.
    /// </summary>
    /// <remarks>These extension methods enable integration of the EonaCat database logger into the logging
    /// pipeline using dependency injection. Use these methods to configure and add database logging capabilities to
    /// your application's logging setup.</remarks>
    public static class DatabaseLoggerFactoryExtensions
    {
        /// <summary>
        /// Adds a database logger provider to the logging builder, allowing log messages to be written to a database.
        /// </summary>
        /// <remarks>Call this method to enable logging to a database using custom configuration. This
        /// method registers a singleton <see cref="ILoggerProvider"/> that writes log entries to the configured
        /// database.</remarks>
        /// <param name="builder">The logging builder to which the database logger provider will be added.</param>
        /// <param name="configure">An action to configure the database logger options. This parameter cannot be null.</param>
        /// <returns>The same instance of <see cref="ILoggingBuilder"/> for chaining.</returns>
        public static ILoggingBuilder AddEonaCatDatabaseLogger(this ILoggingBuilder builder, Action<DatabaseLoggerOptions> configure)
        {
            var options = new DatabaseLoggerOptions();
            configure?.Invoke(options);

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