namespace Brizco.Repository.MartenHandlers.Notifications;

public class CreateNotificationHandlerCommand(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService) 
    : IRequestHandler<CreateNotificationCommand,Guid>
{
    public async Task<Guid> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
    {
        if (currentUserService.ComplexId == null)
            throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is null");
        if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
            throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is wrong");
        var notification = new Notification
        {
            Message = request.Message,
            UserId = request.UserId,
            ComplexId = complexId,
            CreatedAt = DateTime.Now
        };

        await martenRepositoryWrapper.SetRepository<Notification>()
            .AddOrUpdateEntityAsync(notification, cancellationToken);
        return notification.Id;
    }
}