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

        var taskData = new
        {
            Id = notification.TaskId.Value,
            ProjectId = notification.ProjectId.Value,
            StoryId = notification.StoryId.Value,
            Title = notification.TaskTitle,
            UpdatedAt = DateTime.UtcNow
        };

        await notificationService.NotifyTaskUpdated(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.StoryId.Value,
            notification.TaskId.Value,
            taskData);

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