<?php
namespace API\Hiveo\Traits;

trait Relations
{
    

    public function getRevisionId(string $siret)
    {


        $response = $this->client->get($this->urlApi.'api/collectors/'.$this->siretDemandeur.'/relations?depositors='.$siret, [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    
                ],
            ]);
        $this->code = $response->getStatusCode();
        $this->header = $response->getHeaderLine('application/pdf');
        
        $this->setData(json_decode($response->getBody()->getContents(), true));

        return $this->data['content'][0]['state']['dossiers']['0']['documents']['0']['revisionId'];
    }
    
    public function relations(string $siret)
    {

        $response = $this->client->get($this->urlApi.'api/collectors/'.$this->siretDemandeur.'/relations?depositors='.$siret, [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    
                ],
            ]);
            
            
        $this->code = $response->getStatusCode();
        $this->header = $response->getHeaderLine('application/pdf');
        
        $this->setData(json_decode($response->getBody()->getContents(), true));

        return $this->data;
    }
    
    public function relationsDocument(string $siret)
    {

        $response = $this->client->get($this->urlApi.'api/collectors/'.$this->siretDemandeur.'/relations?depositors='.$siret, [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    
                ],
            ]);
            
            
        $this->code = $response->getStatusCode();
        $this->header = $response->getHeaderLine('application/pdf');
        
        $this->setData(json_decode($response->getBody()->getContents(), true));

        $return = [];
        
        foreach ($this->data['content'][0]['state']['dossiers']?? [] as $documents) {
            foreach ($documents['documents'] as $document) {
                if (isset($document['revisionId']) && $document['revisionId'] != '') {
                    $return[$document['documentType']['code']]=$document;
                }
            }
        }
        
        $return['actif'] = $this->data['content'][0]['depositor']['additionalInformation']['contacts']['withActive']??'';
        
        return $return;
    }

    public function getFournisseurNoInscrit($idFournisseur = '')
    {
        try {
            if ($idFournisseur) {
                $idFournisseur = '?depositors=FR-'.$idFournisseur;
            } else {
                $idFournisseur = '?onlyNoRegisteredContact=true';
            }
            
            $response = $this->client->get($this->urlApi.'api/collectors/'.$this->siretDemandeur.'/relations'.$idFournisseur, [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    
                ],
            ]);
            $this->code = $response->getStatusCode();
            
            return $this->setData(json_decode($response->getBody()->getContents(), true));
        } catch (\Exception $e) {
            return [];
        }
    }
}
