using EonaCat.LogStack.Configuration;
using EonaCat.LogStack.Core;
using EonaCat.LogStack.Logging;
using Microsoft.Extensions.Hosting;
using System;

namespace EonaCat.LogStack.Extensions;

/// <summary>
/// Extension methods for integrating EonaCat LogStack with HostApplicationBuilder
/// </summary>
public static class HostApplicationBuilderExtensions
{
    /// <summary>
    /// Adds EonaCat LogStack logging with a configuration callback
    /// </summary>
    public static IHostApplicationBuilder AddEonaCatLogging(
        this IHostApplicationBuilder builder, 
        Action<EonaCatLogStack>? configure = null)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.Services.AddEonaCatLogging(configure ?? (_ => { }));
        return builder;
    }

    /// <summary>
    /// Adds EonaCat LogStack logging with fluent LogBuilder configuration
    /// </summary>
    public static IHostApplicationBuilder AddEonaCatLogging(
        this IHostApplicationBuilder builder,
        Action<LogBuilder> configure)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (configure == null)
        {
            throw new ArgumentNullException(nameof(configure));
        }

        builder.Services.AddEonaCatLogging(configure);
        return builder;
    }

    /// <summary>
    /// Adds EonaCat LogStack logging with fluent LogBuilder configuration and a specific category
    /// </summary>
    public static IHostApplicationBuilder AddEonaCatLogging(
        this IHostApplicationBuilder builder,
        string category,
        Action<LogBuilder> configure)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (configure == null)
        {
            throw new ArgumentNullException(nameof(configure));
        }

        builder.Services.AddEonaCatLogging(category, configure);
        return builder;
    }

    /// <summary>
    /// Adds EonaCat LogStack with AdvancedLoggerFactory for enhanced features
    /// </summary>
    public static IHostApplicationBuilder AddAdvancedEonaCatLogging(
        this IHostApplicationBuilder builder,
        LogLevel minimumLevel = LogLevel.Trace,
        TimestampMode timestampMode = TimestampMode.Utc)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.Services.AddAdvancedEonaCatLogging(minimumLevel, timestampMode);
        return builder;
    }

    /// <summary>
    /// Adds EonaCat LogStack with AdvancedLoggerFactory and a configuration callback
    /// </summary>
    public static IHostApplicationBuilder AddAdvancedEonaCatLogging(
        this IHostApplicationBuilder builder,
        Action<AdvancedLoggerFactory> configure)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (configure == null)
        {
            throw new ArgumentNullException(nameof(configure));
        }

        builder.Services.AddAdvancedEonaCatLogging(configure);
        return builder;
    }

    /// <summary>
    /// Adds the AdvancedMetricsCollector for system-wide analytics
    /// </summary>
    public static IHostApplicationBuilder AddEonaCatMetricsCollection(
        this IHostApplicationBuilder builder)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.Services.AddEonaCatMetricsCollection();
        return builder;
    }
}
