using Microsoft.Extensions.Logging;
using OmsorgCore.Application.Abstractions;
using OmsorgCore.Engine.Events;

namespace OmsorgCore.Engine.Handlers;

public class PasswordResetRequestedHandler : IDomainEventHandler<PasswordResetRequestedEvent>
{
    private readonly IEmailSender _emailSender;
    private readonly IPasswordResetEmailComposer _emailComposer;
    private readonly ILogger<PasswordResetRequestedHandler> _logger;

    public PasswordResetRequestedHandler(
        IEmailSender emailSender,
        IPasswordResetEmailComposer emailComposer,
        ILogger<PasswordResetRequestedHandler> logger)
    {
        _emailSender = emailSender;
        _emailComposer = emailComposer;
        _logger = logger;
    }

    public async Task HandleAsync(PasswordResetRequestedEvent domainEvent, CancellationToken cancellationToken = default)
    {
        var message = _emailComposer.Compose(domainEvent.ToEmail, domainEvent.RawPin);
        await _emailSender.SendAsync(message, cancellationToken);

        _logger.LogInformation("Engine: Passwort-Reset-Code an {Email} versendet ({OccurredAt}).",
            domainEvent.ToEmail, domainEvent.OccurredAt);
    }
}
