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.Discounts;
using Netina.Repository.Repositories.Base.Contracts;

namespace Netina.Repository.Handlers.Discounts;

public class GetDiscountQueryHandler : IRequestHandler<GetDiscountQuery, DiscountLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetDiscountQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<DiscountLDto> Handle(GetDiscountQuery request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<Discount>().TableNoTracking
            .Where(b => b.Id == request.Id)
            .Select(DiscountMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("Discount NotFound", ApiResultStatusCode.NotFound);

        return ent;
    }
}