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

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

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

        var (page, pageSize) = Pagination.Normalize(request.Page, request.PageSize);
        var result = await notifications.ListMineAsync(userId, page, pageSize, cancellationToken);
        return OperationResult<PagedResult<NotificationDto>>.SuccessResult(result);
    }
}
