using EonaCat.LogStack.Core;
using System;
using System.Diagnostics;

namespace EonaCat.LogStack.Boosters;

// 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>
/// Performance monitoring booster that adds timing and resource metrics
/// </summary>
public sealed class PerformanceBooster : BoosterBase
{
    private readonly long _startTime = Stopwatch.GetTimestamp();
    private long _totalMemorySnapshot;

    public PerformanceBooster() : base("Performance") { }

    public override bool Boost(ref LogEventBuilder builder)
    {
        try
        {
            var currentProcess = Process.GetCurrentProcess();
            var elapsed = (long)((DateTime.UtcNow.Ticks - _startTime) / 10000.0);

            builder.WithProperty("uptime_ms", elapsed);
            builder.WithProperty("working_set_mb", currentProcess.WorkingSet64 / (1024 * 1024));
            builder.WithProperty("virtual_memory_mb", currentProcess.VirtualMemorySize64 / (1024 * 1024));
            builder.WithProperty("gc_total_memory", GC.GetTotalMemory(false) / 1024);
            builder.WithProperty("thread_count", Process.GetCurrentProcess().Threads.Count);

            return true;
        }
        catch
        {
            return true; // Don't filter on error
        }
    }
}
