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

// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.

namespace EonaCat.Logger.EonaCatCoreLogger.Extensions
{
    /// <summary>
    /// Provides extension methods for configuring ElasticSearch logging for an <see cref="ILoggingBuilder"/> instance.
    /// </summary>
    public static class ElasticSearchLoggerFactoryExtensions
    {
        /// <summary>
        /// Adds an ElasticSearch-based logger to the logging builder, allowing log messages to be sent to an
        /// ElasticSearch instance.
        /// </summary>
        /// <remarks>Call this method during application startup to enable logging to ElasticSearch. The
        /// provided configuration delegate allows customization of connection settings and logging behavior.</remarks>
        /// <param name="builder">The logging builder to which the ElasticSearch logger will be added.</param>
        /// <param name="configure">A delegate to configure the options for the ElasticSearch logger. Cannot be null.</param>
        /// <returns>The same instance of <see cref="ILoggingBuilder"/> for chaining.</returns>
        public static ILoggingBuilder AddEonaCatElasticSearchLogger(this ILoggingBuilder builder, Action<ElasticSearchLoggerOptions> configure)
        {
            var options = new ElasticSearchLoggerOptions();
            configure?.Invoke(options);
            builder.Services.AddSingleton<ILoggerProvider>(new ElasticSearchLoggerProvider(options));
            return builder;
        }
    }
}
