<?php

namespace auctionforge\inc;

use auctionforge\inc\helpers\SettingsHelper;

class BidSpiritRequest
{
    /**
     * Send request to Bidspirit API. Example https://subdomain.bidspirit.com/api/thirdparty/v1/;
     * @param $action
     * @param $data
     * @return array
     */
    public static function send($action, $data)
    {
        $apiUrl = SettingsHelper::getOption('server', '');
        $serverUri = $apiUrl . '/api/thirdparty/v1/' . $action;
        $serverUri = preg_replace("/\/\//", "/", $serverUri);
        $serverUri = preg_replace('/http(s)?:\/+/', 'https://', $serverUri);

        $params = http_build_query($data);
        $curlInit = curl_init();
        curl_setopt_array($curlInit, array(
            CURLOPT_URL => $serverUri . '?' . $params,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => defined('AUCTIONFORGE_CURL_TIMEOUT') ? AUCTIONFORGE_CURL_TIMEOUT : 30,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));
        $response = curl_exec($curlInit);
        $codes = curl_getinfo($curlInit);
        curl_close($curlInit);

        return [
            'response' => $response,
            'serverUri' => $serverUri,
            'params' => $params,
            'url' => $serverUri . '?' . $params,
            'codes' => $codes
        ];
    }
}