<?php

namespace API\Hiveo;

use Helpers\Session\ParseBase1;

class Hiveo
{

   
    protected string $urlApi;
    protected \GuzzleHttp\Client $client;
    protected mixed $data;
    protected string $token;
    protected int $code;
    protected string $header;
    protected string $siretDemandeur;

    use \API\Hiveo\Traits\Relations;
    use \API\Hiveo\Traits\Documents;
    use \API\Hiveo\Traits\Invitation;
    use \API\Hiveo\Traits\Reminds;

    public function __construct(\PDO $pdo)
    {
        $grandType = 'password';
        try {
            $codeAPI = 'HIVEO';
            $dilispec = new \Entity\Model\Dilispec($pdo);
            $dilispec = $dilispec->findAll(['TYPEDICO' => $codeAPI]);
            foreach ($dilispec as $param) {
                ${$param['CODEDICO']} = $param['LIB01DICO'];
            }
            
            $parametrage = file_get_contents(str_replace('/', '\\', (new ParseBase1())->getRep()->etc.'API/externe/'.$codeAPI.'/Parametres.json'), true);
            if ($parametrage === false) {
                throw new \Exception('invalid file');
            }
            $parametrage = json_decode($parametrage, true);

            /** @phpstan-ignore-next-line */
            $this->siretDemandeur = 'FR-'.$SIRET_CONNEXION;

            /** @phpstan-ignore-next-line */
            $this->setUrlApi($parametrage[$TYPE_ENVIRONNEMENT]['BaseUrl']);

            $this->client = new \GuzzleHttp\Client(['verify' => false]);

            /** @phpstan-ignore-next-line */
            $response = $this->client->post($parametrage[$TYPE_ENVIRONNEMENT]['UrlAuth'], [
            
        
                    'form_params' => [
                        /** @phpstan-ignore-next-line */
                        'client_secret' => $SECRETID,
                        /** @phpstan-ignore-next-line */
                        'client_id' => $IDCLIENT,
                        'grant_type' => $grandType,
                        
                        /** @phpstan-ignore-next-line */
                        'username' => $USER,
                        
                        /** @phpstan-ignore-next-line */
                        'password' => $MDP
                    ]
                ]);

            
            $this->setData(json_decode($response->getBody()->getContents(), true));
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            $returnLog='date : '.date("Y-m-d H:i:s").'
            ';
            
            /** @phpstan-ignore-next-line */
            $returnLog.= 'url : '.$parametrage[$TYPE_ENVIRONNEMENT]['UrlAuth'].'
            
            code : '.$e->getResponse()->getStatusCode().'

            body envoyé : '.json_encode([
                'form_params' => [
                    
                        /** @phpstan-ignore-next-line */
                    'client_secret' => $SECRETID,
                    
                        /** @phpstan-ignore-next-line */
                    'client_id' => $IDCLIENT,
                    'grant_type' => $grandType,
                    
                        /** @phpstan-ignore-next-line */
                    'username' => $USER,
                    
                        /** @phpstan-ignore-next-line */
                    'password' => $MDP
                ]]).'

            Api erreur: ' . $e->getResponse()->getBody()->getContents();
            \Helpers\Fichier\Fichier::createFichierTmp('Log_Hiveo_Auth.txt', $returnLog);
            return;
        }


        if (isset($this->data['access_token'])) {
            $this->setToken($this->data['access_token']);
        }
    }

    public function setUrlApi(string $url) :void
    {
        $this->urlApi = $url;
    }

    public function setData(mixed $data) :mixed
    {
        return $this->data = $data;
    }

    public function setToken(string $token) :void
    {
        $this->token = $token;
    }

    public function getCode() :int
    {
        return $this->code;
    }

    public function getHeader() :string
    {
        return $this->header;
    }

//_____________________________________________________________________________
// Sub-routine
    
    public static function convertDateToSql(string $s) :string
    {
        return substr(
            str_replace('-', '', $s),
            0,
            8
        );
    }
}
