using EonaCat.LogStack.Core;
using EonaCat.LogStack.Flows;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EonaCat.LogStack.Extensions
{
    // 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>
    /// Extension methods for the new cool logging features
    /// </summary>
    public static class CoolFeaturesExtensions
    {
        /// <summary>
        /// Add a filter preset flow to route specific log types
        /// </summary>
        public static LogFilterPresetFlow AddFilteredFlow(
            this IFlow targetFlow,
            string presetName)
        {
            var preset = LogFilterPresetRegistry.Get(presetName) 
                ?? throw new ArgumentException($"Preset '{presetName}' not found", nameof(presetName));
            return new LogFilterPresetFlow(targetFlow, preset);
        }

        /// <summary>
        /// Add a custom filter preset flow
        /// </summary>
        public static LogFilterPresetFlow AddCustomFilterFlow(
            this IFlow targetFlow,
            string name,
            Func<LogEvent, bool> predicate)
        {
            var preset = new CustomPreset(name, predicate);
            return new LogFilterPresetFlow(targetFlow, preset);
        }

        /// <summary>
        /// Generate a heatmap visualization of stored logs
        /// </summary>
        public static string GenerateVisualization(
            this LocalStorageQueryFlow queryFlow,
            VisualizationType type = VisualizationType.Timeline,
            int? width = null,
            int? height = null)
        {
            var events = queryFlow.AllEvents;

            return type switch
            {
                VisualizationType.Timeline => LogHeatmapAnalyzer.GenerateTimelineHeatmap(
                    events, width: width ?? 60, height: height ?? 10),
                VisualizationType.LevelDistribution => LogHeatmapAnalyzer.GenerateLevelDistributionHeatmap(
                    events, width: width ?? 50),
                VisualizationType.CategoryDistribution => LogHeatmapAnalyzer.GenerateCategoryHeatmap(
                    events, width: width ?? 50, topN: height ?? 10),
                VisualizationType.ActivityMatrix => LogHeatmapAnalyzer.GenerateActivityMatrixHeatmap(events),
                VisualizationType.Compact => LogHeatmapAnalyzer.GenerateCompactHeatmap(
                    events, buckets: width ?? 40),
                _ => "Unknown visualization type"
            };
        }

        /// <summary>
        /// Print correlation group summary
        /// </summary>
        public static string GetCorrelationSummary(this CorrelationGroup group)
        {
            return $"CorrelationGroup [ID: {group.CorrelationId}, Events: {group.Events.Count}, Duration: {group.Time().TotalMilliseconds:F0}ms]";
        }

        /// <summary>
        /// Print performance anomaly details
        /// </summary>
        public static string GetAnomalySummary(this PerformanceAnomaly anomaly)
        {
            return anomaly.ToString();
        }

        /// <summary>
        /// Get available filter presets
        /// </summary>
        public static IEnumerable<string> GetAvailableFilterPresets()
        {
            return LogFilterPresetRegistry.GetAvailablePresets();
        }

        /// <summary>
        /// Print log storage statistics
        /// </summary>
        public static string GetStorageStatsSummary(this LogStorageStatistics stats)
        {
            return stats.ToString();
        }

        /// <summary>
        /// Print anomaly detection statistics
        /// </summary>
        public static string GetAnomalyStatsSummary(this AnomalyDetectionStatistics stats)
        {
            return stats.ToString();
        }
    }

    /// <summary>
    /// Types of visualizations available
    /// </summary>
    public enum VisualizationType
    {
        Timeline,
        LevelDistribution,
        CategoryDistribution,
        ActivityMatrix,
        Compact
    }
}
