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 registering a JSON file logger with an <see cref="ILoggingBuilder"/>.
    /// </summary>
    /// <remarks>These extension methods enable logging to JSON-formatted files by adding the necessary logger
    /// provider to the application's dependency injection container. Use these methods to configure and enable JSON
    /// file logging in ASP.NET Core or other .NET applications that use Microsoft.Extensions.Logging.</remarks>
    public static class JsonFileLoggerFactoryExtensions
    {
        /// <summary>
        /// Adds a JSON file logger to the logging builder with the specified configuration options.
        /// </summary>
        /// <remarks>This method registers a singleton <see cref="ILoggerProvider"/> that writes log
        /// entries to a JSON file using the specified options. Call this method during application startup to enable
        /// JSON file logging.</remarks>
        /// <param name="builder">The logging builder to which the JSON file logger will be added.</param>
        /// <param name="configure">An action to configure the options for the JSON file logger. Cannot be null.</param>
        /// <returns>The same instance of <see cref="ILoggingBuilder"/> for chaining.</returns>
        public static ILoggingBuilder AddEonaCatJsonFileLogger(this ILoggingBuilder builder, Action<JsonFileLoggerOptions> configure)
        {
            var options = new JsonFileLoggerOptions();
            configure?.Invoke(options);

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