using DbFirst.API.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace DbFirst.API.Dashboards;

public class DashboardChangeNotifier : IDashboardChangeNotifier
{
    private readonly IHubContext<DashboardsHub> _hubContext;
    private readonly ILogger<DashboardChangeNotifier> _logger;

    public DashboardChangeNotifier(IHubContext<DashboardsHub> hubContext, ILogger<DashboardChangeNotifier> logger)
    {
        _hubContext = hubContext;
        _logger = logger;
    }

    public async Task NotifyChangedAsync()
    {
        try
        {
            await _hubContext.Clients.All.SendAsync("DashboardsChanged");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to notify dashboard clients.");
        }
    }
}
