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 StoryDeletedEvent - sends SignalR notification
/// </summary>
public class StoryDeletedEventHandler : INotificationHandler<StoryDeletedEvent>
{
    private readonly IProjectNotificationService _notificationService;
    private readonly ILogger<StoryDeletedEventHandler> _logger;

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

    public async Task Handle(StoryDeletedEvent notification, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Handling StoryDeletedEvent for story {StoryId}", notification.StoryId);

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

        _logger.LogInformation("SignalR notification sent for story {StoryId}", notification.StoryId);
    }
}
