using EonaCat.LogStack.Anomalies;
using EonaCat.LogStack.DeadLettering;
using EonaCat.LogStack.Diagnostics;
using EonaCat.LogStack.Routing;
using EonaCat.LogStack.Sampling;
using System;
using System.Collections.Generic;

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

/// <summary>
/// Aggregated statistics from all superior features.
/// </summary>
public class SuperiorFeaturesStats
{
    /// <summary>
    /// Whether intelligent routing is enabled.
    /// </summary>
    public bool IntelligentRoutingEnabled { get; set; }

    /// <summary>
    /// Statistics from the intelligent router.
    /// </summary>
    public RouterStats RouterStats { get; set; }

    /// <summary>
    /// Whether adaptive sampling is enabled.
    /// </summary>
    public bool AdaptiveSamplingEnabled { get; set; }

    /// <summary>
    /// Metrics from the adaptive sampler.
    /// </summary>
    public SamplingMetrics SamplingMetrics { get; set; }

    /// <summary>
    /// Whether anomaly detection is enabled.
    /// </summary>
    public bool AnomalyDetectionEnabled { get; set; }

    /// <summary>
    /// Statistics from the anomaly detector.
    /// </summary>
    public AnomalyDetectorStats AnomalyStats { get; set; }

    /// <summary>
    /// Recent anomalies that were detected.
    /// </summary>
    public List<AnomalyAlert> RecentAnomalies { get; set; }

    /// <summary>
    /// Whether context snapshots are enabled.
    /// </summary>
    public bool ContextSnapshotsEnabled { get; set; }

    /// <summary>
    /// Statistics from the context snapshot collector.
    /// </summary>
    public SnapshotCollectorStats SnapshotStats { get; set; }

    /// <summary>
    /// Whether the dead letter queue is enabled.
    /// </summary>
    public bool DeadLetterQueueEnabled { get; set; }

    /// <summary>
    /// Statistics from the dead letter queue.
    /// </summary>
    public DeadLetterQueueStats DlqStats { get; set; }

    /// <summary>
    /// Failure reasons grouped by count.
    /// </summary>
    public Dictionary<string, int> DlqFailureReasons { get; set; }

    /// <summary>
    /// Gets a summary string of all enabled features.
    /// </summary>
    public string GetEnabledFeaturesSummary()
    {
        var features = new List<string>();

        if (IntelligentRoutingEnabled)
            features.Add($"IntelligentRouting({RouterStats?.TotalRules ?? 0} rules)");

        if (AdaptiveSamplingEnabled)
            features.Add($"AdaptiveSampling({SamplingMetrics?.SamplingRate:P1} rate)");

        if (AnomalyDetectionEnabled)
            features.Add($"AnomalyDetection({AnomalyStats?.RecentAnomalies ?? 0} recent)");

        if (ContextSnapshotsEnabled)
            features.Add($"ContextSnapshots({SnapshotStats?.CurrentSnapshots ?? 0} snapshots)");

        if (DeadLetterQueueEnabled)
            features.Add($"DeadLetterQueue({DlqStats?.CurrentQueueSize ?? 0}/{DlqStats?.MaxCapacity ?? 0})");

        return "Superior Features: " + (features.Count > 0 
            ? string.Join(", ", features) 
            : "None enabled");
    }
}
