using System.Diagnostics;
using System.Text.Json;

namespace NATS.Server.Monitoring;

/// <summary>
/// Lightweight profiling endpoint handler with Go-compatible route shapes.
/// </summary>
public sealed class PprofHandler
{
    public string Index()
    {
        return """
               profiles:
               - profile
               - heap
               - goroutine
               - threadcreate
               - block
               - mutex
               """;
    }

    public byte[] CaptureCpuProfile(int seconds)
    {
        var boundedSeconds = Math.Clamp(seconds, 1, 120);
        var payload = JsonSerializer.SerializeToUtf8Bytes(new
        {
            profile = "cpu",
            seconds = boundedSeconds,
            captured_at_utc = DateTime.UtcNow,
            process_total_cpu_ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds,
            thread_count = Process.GetCurrentProcess().Threads.Count,
        });
        return payload;
    }
}
