<?php

namespace API\Hiveo\Traits;

trait Invitation
{
    
    
   
    public function invitationDepositor(string $siren, string $name, string $email) :mixed
    {
        
       //$siren = 'FR-45328087700045';
         //$email = 'colin@dalaska.com';

         $body ='{
            "guests": [
              {
                "email": "'.$email.'",
                "firstName": "'.str_replace(',', '', $name).'"
              }
            ],
            "internalCode": "",
            "invitedOrganisationInternalId": "'.$siren.'",
            "invitingOrganisationInternalId": "'.$this->siretDemandeur.'"
          }';
        
        try {
            $response = $this->client->post($this->urlApi.'api/invite-depositor', [
     
            'headers' => [
                'Authorization' => 'Bearer '. $this->token,
                'Content-Type' => 'application/json',
            ],'body' => $body]);

            $this->code = $response->getStatusCode();
            $this->header = $response->getHeaderLine('content-type');
            $this->setData(json_decode($response->getBody()->getContents(), true));
            return (string)$this->code;
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            $returnLog='date : '.date("Y-m-d H:i:s").'
            url : '.$this->urlApi.'api/invite-depositor'.'

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

            body envoyé : '.$body.'

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

    public function closeRelation(string $siren) :mixed
    {
        
       //$siren = 'FR-45328087700045';
        // $email = 'colin@dalaska.com';

        $body =json_encode([
            "collector" =>$this->siretDemandeur,
            "depositor" => $siren
        ]);
        
        try {
            $response = $this->client->post($this->urlApi.'api/collectors/'.$this->siretDemandeur.'/close-relation', [
     
            'headers' => [
                'Authorization' => 'Bearer '. $this->token,
                'Content-Type' => 'application/json',
            ],'body' => $body]);

            $this->code = $response->getStatusCode();
            $this->header = $response->getHeaderLine('content-type');
            $this->setData(json_decode($response->getBody()->getContents(), true));
        
            return $this->code;
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            $returnLog='date : '.date("Y-m-d H:i:s").'
            url : '.$this->urlApi.'api/collectors/'.$this->siretDemandeur.'/close-relation'.'

            body envoyé : '.$body.'

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

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