using MediatR;
using Microsoft.Extensions.Logging;
using ColaFlow.Modules.ProjectManagement.Domain.Events;
using ColaFlow.Modules.ProjectManagement.Application.Services;

namespace ColaFlow.Modules.ProjectManagement.Application.EventHandlers;

/// <summary>
/// Handler for EpicDeletedEvent - sends SignalR notification
/// </summary>
public class EpicDeletedEventHandler : INotificationHandler<EpicDeletedEvent>
{
    private readonly IProjectNotificationService _notificationService;
    private readonly ILogger<EpicDeletedEventHandler> _logger;

    public EpicDeletedEventHandler(
        IProjectNotificationService notificationService,
        ILogger<EpicDeletedEventHandler> logger)
    {
        _notificationService = notificationService;
        _logger = logger;
    }

    public async Task Handle(EpicDeletedEvent notification, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Handling EpicDeletedEvent for epic {EpicId}", notification.EpicId);

        await _notificationService.NotifyEpicDeleted(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.EpicId.Value);

        _logger.LogInformation("SignalR notification sent for epic {EpicId}", notification.EpicId);
    }
}
