<?php

namespace App\Services\Integration\Integration;

use App\Services\Integration\Interface\APIInterface;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

class API implements APIInterface
{

    public static function getRequest($url,$data)
    {
        $secret = Config::get('app.secret');
        $response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
            'Authorization' => 'Bearer '.$secret,
        ])->get($url, $data);
        return $response;
    }

    public static function getItemRequest($url)
    {
        $response = Http::withHeaders([
            'Content-Type' => 'application/json',
        ])->get($url);
        return $response;
    }

    public static function postRequest($url,$data)
    {
        $secret = Config::get('app.secret');
        $response = Http::withHeaders([
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer '.$secret,
            ])->post($url, $data);
        return $response;
    }

    public static function putRequest($url,$data)
    {
        $response = Http::withHeaders([
                'Content-Type' => 'application/json',
            ])->put($url, $data);
        return $response;
    }

    public static function deleteRequest($url)
    {
        $response = Http::withHeaders([
            'Content-Type' => 'application/json',
        ])->delete($url);
        return $response;
    }

    public static function executeRequest($url, $data= [], $type= "GET")
    {

        $curl = curl_init();
        $secret = Config::get('app.secret');
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $type ?? 'GET',
            CURLOPT_HTTPHEADER => array(
                'Accept: application/json',
                'Authorization: Bearer '.$secret
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return $response;

    }

}
