using Microsoft.Extensions.Logging;

namespace ClawdDotNet.Core.Logging;

public static class LoggingExtensions
{
    public static ILoggerFactory AddClawdFileLogging(
        this ILoggerFactory factory,
        FileLoggerOptions? options = null)
    {
        factory.AddProvider(new FileLoggerProvider(options ?? new FileLoggerOptions()));
        return factory;
    }

    public static ILoggerFactory CreateClawdLoggerFactory(
        string logDirectory = "./Logs",
        LogLevel minimumLevel = LogLevel.Info)
    {
        var options = new FileLoggerOptions
        {
            LogDirectory = logDirectory,
            MinimumLevel = minimumLevel
        };

        var factory = LoggerFactory.Create(builder =>
        {
            builder.SetMinimumLevel(MapToMelLevel(minimumLevel));
        });

        factory.AddClawdFileLogging(options);
        return factory;
    }

    private static Microsoft.Extensions.Logging.LogLevel MapToMelLevel(LogLevel level) => level switch
    {
        LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
        LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
        LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
        LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
        _ => Microsoft.Extensions.Logging.LogLevel.Information
    };
}
