using AyCode.Services.Nav.Ekaer;
using AyCode.Services.Nav.Ekaer.Models;
using FruitBank.Common.Entities;
using FruitBank.Common.Services.Ekaer;

namespace FruitBank.Common.Server.Services.Ekaer;

/// <inheritdoc cref="IFruitBankEkaerService"/>
/// <remarks>
/// A teljes lánc: <c>map</c> (<see cref="IShippingToEkaerMapper"/>, FruitBank.Common) →
/// <c>validate → send</c> (<see cref="IEkaerSubmitService"/>, AyCode.Services). A NAV-fiók hitelesítő adatait
/// (<c>INavCredentials</c>) és a saját cégadatokat (<see cref="EkaerCompanyInfo"/>) a DI szolgáltatja.
/// </remarks>
public sealed class FruitBankEkaerService : IFruitBankEkaerService
{
    private readonly IShippingToEkaerMapper _mapper;
    private readonly IEkaerSubmitService _submitService;
    private readonly EkaerCompanyInfo _company;

    public FruitBankEkaerService(IShippingToEkaerMapper mapper, IEkaerSubmitService submitService, EkaerCompanyInfo company)
    {
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
        _submitService = submitService ?? throw new ArgumentNullException(nameof(submitService));
        _company = company ?? throw new ArgumentNullException(nameof(company));
    }

    public Task<EkaerSubmitResult> SubmitShippingAsync(Shipping shipping, OperationType operation = OperationType.Create, CancellationToken cancellationToken = default)
    {
        ArgumentNullException.ThrowIfNull(shipping);

        // map (FruitBank.Common) → submit: validate → send (AyCode.Services)
        var operations = _mapper.MapShipping(shipping, _company, operation);
        return _submitService.SubmitAsync(operations, cancellationToken);
    }
}
