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

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

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

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