using AutoMapper;
using DbFirst.Application.Repositories;
using DbFirst.Contracts.MassData;
using MediatR;

namespace DbFirst.Application.MassData.Commands;

public class UpsertMassDataByCustomerNameHandler : IRequestHandler<UpsertMassDataByCustomerNameCommand, MassDataReadDto>
{
    private readonly IMassDataRepository _repository;
    private readonly IMapper _mapper;

    public UpsertMassDataByCustomerNameHandler(IMassDataRepository repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    public async Task<MassDataReadDto> Handle(UpsertMassDataByCustomerNameCommand request, CancellationToken cancellationToken)
    {
        var dto = request.Dto;
        var updated = await _repository.UpsertByCustomerNameAsync(dto.CustomerName, dto.Amount, dto.StatusFlag, dto.Category, cancellationToken);
        return _mapper.Map<MassDataReadDto>(updated);
    }
}
