using Microsoft.EntityFrameworkCore;
using Netina.Common.Models.Api;
using Netina.Common.Models.Exception;
using Netina.Domain.CommandQueries.Queries;
using Netina.Domain.Dtos.LargDtos;
using Netina.Domain.Entities.Products;
using Netina.Repository.Repositories.Base.Contracts;

namespace Netina.Repository.Handlers.Reviews;

public class GetReviewQueryHandler : IRequestHandler<GetReviewQuery,ReviewLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetReviewQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<ReviewLDto> Handle(GetReviewQuery request, CancellationToken cancellationToken)
    {
        var review = await _repositoryWrapper.SetRepository<Review>()
            .TableNoTracking
            .Where(r => r.Id == request.Id)
            .Select(ReviewMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (review == null)
            throw new AppException("Review not found", ApiResultStatusCode.NotFound);

        return review;
    }
}