using System;
using System.Collections.Generic;
using EonaCat.LogStack.Filtering;
using EonaCat.LogStack.Output.Formatters;
using EonaCat.LogStack.Output.Layouts;
using EonaCat.LogStack.Structured;
using EonaCat.LogStack.Templates;

namespace EonaCat.LogStack.Configuration;

// 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.

/// <summary>
/// Advanced logger configuration settings
/// </summary>
public sealed class LoggerConfiguration
{
    private readonly Dictionary<string, object> _config = new();

    /// <summary>
    /// Sets a configuration value
    /// </summary>
    public LoggerConfiguration Set(string key, object value)
    {
        _config[key] = value;
        return this;
    }

    /// <summary>
    /// Gets a configuration value
    /// </summary>
    public T? Get<T>(string key, T? defaultValue = default)
    {
        if (_config.TryGetValue(key, out var value))
        {
            return (T?)value;
        }
        return defaultValue;
    }

    /// <summary>
    /// Enables buffering with specified size
    /// </summary>
    public LoggerConfiguration WithBuffering(int bufferSize = 1000)
    {
        Set("BufferSize", bufferSize);
        return this;
    }

    /// <summary>
    /// Enables batching with specified batch size
    /// </summary>
    public LoggerConfiguration WithBatching(int batchSize = 100)
    {
        Set("BatchSize", batchSize);
        return this;
    }

    /// <summary>
    /// Sets the maximum pool size for object reuse
    /// </summary>
    public LoggerConfiguration WithPooling(int maxPoolSize = 10000)
    {
        Set("MaxPoolSize", maxPoolSize);
        return this;
    }

    /// <summary>
    /// Enables diagnostics collection
    /// </summary>
    public LoggerConfiguration WithDiagnostics(bool enabled = true)
    {
        Set("DiagnosticsEnabled", enabled);
        return this;
    }

    /// <summary>
    /// Enables context propagation using AsyncLocal
    /// </summary>
    public LoggerConfiguration WithContextPropagation(bool enabled = true)
    {
        Set("ContextPropagationEnabled", enabled);
        return this;
    }

    /// <summary>
    /// Sets sampling rate (0-1, where 1 = log all events)
    /// </summary>
    public LoggerConfiguration WithSampling(double rate)
    {
        var clampedRate = rate < 0.0 ? 0.0 : (rate > 1.0 ? 1.0 : rate);
        Set("SamplingRate", clampedRate);
        return this;
    }

    /// <summary>
    /// Sets rate limit (events per second)
    /// </summary>
    public LoggerConfiguration WithRateLimit(int eventsPerSecond)
    {
        Set("RateLimit", eventsPerSecond);
        return this;
    }

    public Dictionary<string, object> Build()
    {
        return new Dictionary<string, object>(_config);
    }
}

/// <summary>
/// Predefined logger presets
/// </summary>
public enum LoggerPreset
{
    Development,
    Production,
    Diagnostics,
    Performance
}
