using Femto.Modules.Auth.Data;
using Femto.Modules.Auth.Models.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Femto.Modules.Auth.Models.DomainEventHandlers;

internal class UserPasswordChangedHandler(AuthContext context)
    : INotificationHandler<UserWasCreatedEvent>
{
    public async Task Handle(UserWasCreatedEvent notification, CancellationToken cancellationToken)
    {
        var longTermSessions = await context
            .LongTermSessions.Where(s => s.UserId == notification.User.Id)
            .ToListAsync(cancellationToken);
        
        foreach (var session in longTermSessions)
        {
            session.Invalidate();
        }
    }
}
