#nullable enable
using Baya.Application.Contracts.Common;
using Baya.Application.Contracts.Persistence;
using Baya.Application.Models.Common;
using Baya.Application.Models.Identity;
using Mediator;

namespace Baya.Application.Features.Identity.Queries.GetMyNurseProfile;

internal sealed class GetMyNurseProfileQueryHandler(ICurrentUser currentUser, IUnitOfWork unitOfWork)
    : IRequestHandler<GetMyNurseProfileQuery, OperationResult<NurseProfileDto>>
{
    public async ValueTask<OperationResult<NurseProfileDto>> Handle(GetMyNurseProfileQuery request, CancellationToken cancellationToken)
    {
        if (currentUser.UserId is not { } userId)
            return OperationResult<NurseProfileDto>.UnauthorizedResult("Not authenticated.");

        var dto = await unitOfWork.NurseProfileRepository.GetMineAsync(userId, cancellationToken);
        return dto is null
            ? OperationResult<NurseProfileDto>.NotFoundResult("No nurse profile exists yet.")
            : OperationResult<NurseProfileDto>.SuccessResult(dto);
    }
}
