using Leanetec.EConnect.Client.Dto;
using Leanetec.EConnect.Client.Interface;
using Leanetec.EConnect.Domain.Entities;
using MediatR;

namespace Leanetec.EConnect.Client.Order;

public record PostDocumentRequest(string TenantId, int OrderId) : HttpRequest<Response<ProblemDetail>>
{
    public UploadDocumentRequest ToUploadDocument(Stream stream, string fileName)
    {
        return new UploadDocumentRequest(this, stream, fileName);
    }
}

public record UploadDocumentRequest(PostDocumentRequest Original, Stream Stream, string FileName) : PostDocumentRequest(Original);

public class PostDocumentRequestHandler : IRequestHandler<UploadDocumentRequest, Response<ProblemDetail>>
{
    private readonly IEConnectClient<ProblemDetail> _client;

    public PostDocumentRequestHandler(IEConnectClient<ProblemDetail> client)
    {
        _client = client;
    }

    public async Task<Response<ProblemDetail>> Handle(UploadDocumentRequest request, CancellationToken cancel)
    {
        return await _client.PostAsync(
            request.Stream,
            request.FileName,
            $"api/public/v{request.ApiVersion}/econnect/order/document",
            new { request.TenantId, request.OrderId },
            cancel
        );
    }
}