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

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

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

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

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

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