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

        var storyData = new
        {
            Id = notification.StoryId.Value,
            ProjectId = notification.ProjectId.Value,
            EpicId = notification.EpicId.Value,
            Title = notification.StoryTitle,
            CreatedAt = DateTime.UtcNow
        };

        await notificationService.NotifyStoryCreated(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.EpicId.Value,
            notification.StoryId.Value,
            storyData);

        logger.LogInformation("SignalR notification sent for story {StoryId}", notification.StoryId);
    }
}
