using Grand.Api.Commands.Models.Catalog;
using Grand.Api.DTOs.Catalog;
using Grand.Api.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Products;
using MediatR;

namespace Grand.Api.Commands.Handlers.Catalog;

public class
    UpdateProductAttributeMappingCommandHandler : IRequestHandler<UpdateProductAttributeMappingCommand,
    ProductAttributeMappingDto>
{
    private readonly IProductAttributeService _productAttributeService;

    public UpdateProductAttributeMappingCommandHandler(IProductAttributeService productAttributeService)
    {
        _productAttributeService = productAttributeService;
    }

    public async Task<ProductAttributeMappingDto> Handle(UpdateProductAttributeMappingCommand request,
        CancellationToken cancellationToken)
    {
        //insert mapping
        var productAttributeMapping = request.Model.ToEntity();
        await _productAttributeService.UpdateProductAttributeMapping(productAttributeMapping, request.Product.Id, true);

        return productAttributeMapping.ToModel();
    }
}