<?php declare(strict_types=1);

class Agro_Essor_Plug_Formularios
{
    public function execute(Agro_Essor_Plug_Dto_FormulariosResquest $dto): array
    {
        $url = PLUG_URL . '/v1/formularios';
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);

        $header = ['Content-Type: application/json'];
        if (APPLICATION_ENV == 'production') {
            $bearerToken = Agro_Essor_Plug_Auth::login();
            $header[] = "Authorization: Bearer {$bearerToken}";
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dto->toArray()));

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $response = json_decode($response, true);
        
        return [$response, $httpCode];
    }
}