<?php
namespace API\Eattestations;

use Helpers\Session\ParseBase1;

class Eattestations
{

    protected string $codeAPI;
    protected string $urlApi;
    protected \GuzzleHttp\Client $client;
    protected mixed $data;
    protected string $token;
    protected mixed $account;
    protected int $code;
    protected string $header;
    protected \PDO $pdo;

    use \API\Eattestations\Traits\Account;
    use \API\Eattestations\Traits\Document;

    public function __construct(\PDO $pdo)
    {

        
        $this->codeAPI = 'EAT';
        $this->pdo = $pdo;
        $parametrage = file_get_contents(str_replace('/', '\\', (new ParseBase1())->getRep()->etc.'API/externe/'.$this->codeAPI.'/Parametres.json'), true);
        if ($parametrage === false) {
            throw new \Exception('Invalid file');
        }
        $parametrage = json_decode($parametrage, true);
 
        
        $dilispec = new \Entity\Model\Dilispec($this->pdo);
        $dilispec = $dilispec->findAll(['TYPEDICO' => $this->codeAPI]);
        
        foreach ($dilispec as $param) {
            ${$param['CODEDICO']} = $param['LIB01DICO'];
        }

        
        /** @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']."/realms/".$REALM."/protocol/openid-connect/token", [
           
                'form_params' => [
                    /** @phpstan-ignore-next-line */
                    'username' => $USER,
                    /** @phpstan-ignore-next-line */
                    'password' => $MDP,
                    /** @phpstan-ignore-next-line */
                    'client_id' => $CLIENT_ID,
                    'grant_type' => 'password'
                ]

            ]);


        $this->setData(json_decode($response->getBody()->getContents(), true));

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

            $response = $this->client->get($this->urlApi."api/v1/account", [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    'Content-Type' => 'application/json',
                ]
                

            ]);

            $this->setData(json_decode($response->getBody()->getContents(), true));

            if (isset($this->data['content'][0]['id'])) {
                $this->setAccount($this->data['content'][0]['id']);
            }
        }
    }

  

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

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

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

    public function setAccount(mixed $account) :void
    {
        $this->account = $account;
    }

    public function getAccount() :mixed
    {
        return $this->account;
    }

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

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

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