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 : INotificationHandler<TaskAssignedEvent>
{
    private readonly IProjectNotificationService _notificationService;
    private readonly ILogger<TaskAssignedEventHandler> _logger;

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

    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);
    }
}
