using Baya.Application.Contracts.Common;
using Baya.Application.Contracts.Notifications;
using Baya.Application.Models.Common;
using Mediator;

namespace Baya.Application.Features.Notifications.Commands.MarkNotificationRead;

internal sealed class MarkNotificationReadCommandHandler(INotificationService notifications, ICurrentUser currentUser)
    : IRequestHandler<MarkNotificationReadCommand, OperationResult<bool>>
{
    public async ValueTask<OperationResult<bool>> Handle(MarkNotificationReadCommand request, CancellationToken cancellationToken)
    {
        if (currentUser.UserId is not { } userId)
            return OperationResult<bool>.FailureResult("User", "Not authenticated.");

        var marked = await notifications.MarkReadAsync(userId, request.NotificationId, cancellationToken);

        return marked
            ? OperationResult<bool>.SuccessResult(true)
            : OperationResult<bool>.NotFoundResult($"Notification {request.NotificationId} was not found.");
    }
}
