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

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

    public async Task Handle(ProjectCreatedEvent notification, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Handling ProjectCreatedEvent for project {ProjectId}", notification.ProjectId);

        var projectData = new
        {
            Id = notification.ProjectId.Value,
            Name = notification.ProjectName,
            CreatedBy = notification.CreatedBy.Value,
            CreatedAt = DateTime.UtcNow
        };

        await _notificationService.NotifyProjectCreated(
            notification.TenantId.Value,
            notification.ProjectId.Value,
            projectData);

        _logger.LogInformation("SignalR notification sent for project {ProjectId}", notification.ProjectId);
    }
}
