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 EpicCreatedEvent - sends SignalR notification
/// </summary>
public class EpicCreatedEventHandler : INotificationHandler<EpicCreatedEvent>
{
    private readonly IProjectNotificationService _notificationService;
    private readonly ILogger<EpicCreatedEventHandler> _logger;

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

    public async Task Handle(EpicCreatedEvent notification, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Handling EpicCreatedEvent for epic {EpicId}", notification.EpicId);

        var epicData = new
        {
            Id = notification.EpicId.Value,
            ProjectId = notification.ProjectId.Value,
            Name = notification.EpicName,
            CreatedAt = DateTime.UtcNow
        };

        await _notificationService.NotifyEpicCreated(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            notification.EpicId.Value,
            epicData);

        _logger.LogInformation("SignalR notification sent for epic {EpicId}", notification.EpicId);
    }
}
