<?php

declare(strict_types=1);

namespace App\SignDocument\Api;

use App\Infrastructure\External\Api\AbstractApi;
use App\Infrastructure\External\Api\BinaryStringFileResult;
use GuzzleHttp\RequestOptions;

class Api extends AbstractApi
{
    public ApiParams $apiParams;

    public function send(string $token, string $path, int $batch): array
    {
        $params = [
            RequestOptions::HEADERS => [
                'Authorization' => $token,
                'Accept' => 'application/json',
            ],
            RequestOptions::MULTIPART => [
                [
                    'name' => 'file',
                    'contents' => fopen($path, 'r'),
                    'filename' => $path,
                    'headers' => [
                        'Content-Type' => '<Content-type header>',
                    ],
                ],
                [
                    'name' => 'type',
                    'contents' => 'attorney-signed'
                ],
            ],
        ];

        $response = $this->client->post(sprintf('%s%s%s', $this->apiParams->endPointUrl, '/api/v1/document/upload/batch/', $batch), $params);

        return $this->responseHandler->setResponse($response)->getContentJsonToArray();
    }

    public function download(string $url, string $token): BinaryStringFileResult
    {
        $params = [
            RequestOptions::HEADERS => [
                'Authorization' => $token,
            ],
        ];

        $response = $this->client->get($url, $params);

        return $this->responseHandler->setResponse($response)->getContentAsBinaryStingResult();
    }
}