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

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

    public async Task Handle(TaskDeletedEvent notification, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Handling TaskDeletedEvent for task {TaskId}", notification.TaskId);

        await _notificationService.NotifyTaskDeleted(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.StoryId.Value,
            notification.TaskId.Value);

        _logger.LogInformation("SignalR notification sent for task {TaskId}", notification.TaskId);
    }
}
