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 TaskAssignedEvent - sends SignalR notification
/// </summary>
public class TaskAssignedEventHandler(
    IProjectNotificationService notificationService,
    ILogger<TaskAssignedEventHandler> logger)
    : INotificationHandler<TaskAssignedEvent>
{
    public async Task Handle(TaskAssignedEvent notification, CancellationToken cancellationToken)
    {
        logger.LogInformation("Handling TaskAssignedEvent for task {TaskId}", notification.TaskId);

        await notificationService.NotifyTaskAssigned(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.TaskId.Value,
            notification.AssigneeId.Value);

        logger.LogInformation("SignalR notification sent for task {TaskId} assigned to {AssigneeId}",
            notification.TaskId, notification.AssigneeId);
    }
}
