using Knot.Shared.Kernel;
using Knot.Contracts.Profiles.Domain;
using Knot.Contracts.Profiles.Application.DTOs;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Knot.Modules.Profiles.Application.Profiles.GetProfile;

public sealed record GetProfileQuery(Guid UserId) : IQuery<UserProfileDto>;

internal sealed class GetProfileQueryHandler : IQueryHandler<GetProfileQuery, UserProfileDto>
{
    private readonly IProfileRepository _repository;

    public GetProfileQueryHandler(IProfileRepository repository)
    {
        _repository = repository;
    }

    public async Task<Result<UserProfileDto>> Handle(GetProfileQuery request, CancellationToken cancellationToken)
    {
        var profile = await _repository.GetAsync(request.UserId, cancellationToken);
        if (profile is null)
            return Result.Failure<UserProfileDto>(ProfilesErrors.ProfileNotFound);

        return Result.Success(profile);
    }
}
