<?php

namespace API\Eattestations\Traits;

use Helpers\Fichier\Fichier;
use Helpers\Session\ParseBase1;

trait Document
{



    public function getfileDownload(string $dossierId, string $documentId, string $fileId) :string
    {


        $body = '{}';

        $path = (new ParseBase1())->getRep()->tmp.Fichier::filterFileName('file_document_' . $fileId . $documentId . '.pdf');
        $file_path = fopen($path, 'w+');

        try {
            $response = $this->client->get($this->urlApi . 'api/v1/account/' . $this->account . '/dossiers/' . $dossierId . '/documents/' . $documentId . '/files/' . $fileId, [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token,
                    'Accept' => 'application/pdf',
                    'Content-type' => 'application/pdf',

                ],
                'save_to' => $file_path
            ]);

            $this->code = $response->getStatusCode();
            $this->header = $response->getHeaderLine('application/pdf');
            Fichier::createFichierTmp('file_document_' . $fileId . $documentId . '.pdf', $response->getBody()->getContents());
            $this->setData(json_decode($response->getBody()->getContents(), true));

            return $path;
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            $returnLog = 'date : ' . date("Y-m-d H:i:s") . '
            url : ' . $this->urlApi . 'api/v1/account/' . $this->account . '/dossiers/' . $dossierId . '/documents/' . $documentId . '/files/' . $fileId . '

            save_to : ' . $file_path . '

            code : ' . $e->getResponse()->getStatusCode() . '

            body envoyé : ' . $body . '

            Api erreur: ' . $e->getResponse()->getBody()->getContents();
            $this->code = $e->getResponse()->getStatusCode();
            \Helpers\Fichier\Fichier::createFichierTmp('Log_Eattestations_Account_File_Download.txt', $returnLog);
            return (string)$this->code;
        }
    }
}
