using EonaCat.Logger.LogServer.Hubs;
using EonaCat.Logger.Server.Models;
using Microsoft.AspNetCore.SignalR;

namespace EonaCat.Logger.LogServer.Data
{
    public class LogDispatcher
    {
        private readonly IHubContext<LogHub> _hub;

        public LogDispatcher(IHubContext<LogHub> hub)
        {
            _hub = hub;
        }

        public async Task PublishAsync(LogEntry log)
        {
            await _hub.Clients.All.SendAsync("NewLog", new
            {
                id = log.Id,
                level = log.Level,
                message = log.Message,
                applicationName = log.ApplicationName,
                environment = log.Environment,
                timestamp = log.Timestamp,
                machineName = log.MachineName,
                correlationId = log.CorrelationId
            });
        }
    }
}
