using ColaFlow.Modules.Identity.Domain.Aggregates.Users.Events;
using MediatR;
using Microsoft.Extensions.Logging;

namespace ColaFlow.Modules.Identity.Application.EventHandlers;

public sealed class UserRoleAssignedEventHandler(ILogger<UserRoleAssignedEventHandler> logger)
    : INotificationHandler<UserRoleAssignedEvent>
{
    public Task Handle(UserRoleAssignedEvent notification, CancellationToken cancellationToken)
    {
        logger.LogInformation(
            "User {UserId} assigned role {Role} in tenant {TenantId}. Previous role: {PreviousRole}. Assigned by: {AssignedBy}",
            notification.UserId,
            notification.Role,
            notification.TenantId.Value,
            notification.PreviousRole,
            notification.AssignedBy);

        // Future: Add audit log to database
        // Future: Invalidate user permissions cache
        // Future: Send notification to user

        return Task.CompletedTask;
    }
}
