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

namespace Baya.Application.Features.Notifications.Queries.GetUnreadCount;

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

        var count = await notifications.GetUnreadCountAsync(userId, cancellationToken);
        return OperationResult<UnreadCountResult>.SuccessResult(new UnreadCountResult(count));
    }
}
