<?php
namespace API\Hiveo\Traits;

trait Relations
{
    

    public function getRevisionId(string $siret) :mixed
    {


        $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) :mixed
    {

        $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;
    }
    
    /**
     * @return array<mixed>
     */
    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(string $idFournisseur = '') :mixed
    {
        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 [];
        }
    }
}
