using NexusShell.Agent.Models;
using NexusShell.Models;
using NexusShell.Services;

namespace NexusShell.Agent;

public sealed class NexusHealthService
{
    private readonly NexusHardwareService _hardware;

    public NexusHealthService(NexusHardwareService hardware)
    {
        _hardware = hardware;
    }

    public NexusAgentHealthResponse CaptureSnapshot()
    {
        var cpu = _hardware.CaptureCpu();
        var memory = _hardware.CaptureMemory();
        var network = _hardware.CaptureNetwork();
        var primaryFreeGb = _hardware.GetPrimaryFreeGb();
        var health = _hardware.CaptureSystemHealth(cpu, memory, primaryFreeGb, network);

        return new NexusAgentHealthResponse
        {
            CapturedAtUtc = DateTimeOffset.UtcNow,
            Overall = MapOverall(health.OverallStatus),
            HealthPercent = health.HealthPercent,
            Cpu = MapComponent(health.CpuStatus),
            Ram = MapComponent(health.RamStatus),
            Disk = MapComponent(health.DiskStatus),
            Network = MapComponent(health.NetworkStatus),
            Temperature = cpu.TemperatureLabel,
        };
    }

    public string CaptureOverallLabel() => CaptureSnapshot().Overall;

    private static string MapOverall(string status) => status switch
    {
        "Critical" => "Critical",
        "Warning" => "Warning",
        _ => "Healthy",
    };

    private static string MapComponent(string status) => status switch
    {
        "Critical" => "Critical",
        "Warning" => "Warning",
        _ => "Healthy",
    };
}
