<?php

use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Http;


if (! function_exists('reqGestKey')) {
    function reqGestKey($api_key)
    {
        $base_url = "http://192.168.8.179/ProjetsLaravel/GestStudent/GestStudentApi/public/";
        return Http::withHeaders([
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => $api_key,
        ])->post($base_url . 'api/v1/api_key/auth')->json();
    }
}


if (!function_exists('reqGest')) {
    function reqGest($method = 'GET', $url, $body = [], $is_with_client = 1)
    {

        $authorization = '2131566549';
        /**
         * 1 => with api_key only
         * 2 => with api_key AND client id
         */
        $headers = [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ];
        if ($is_with_client == 1) {
            $headers = array_merge($headers, [
                'Authorization' => 'Bearer '.config('app.api_key')//$authorization//config('app.api_key')
            ]);
        } elseif ($is_with_client == 2) { // add here client Key
            $headers = array_merge($headers, [
                'Authorization' => 'Bearer '.config('app.api_key'),//$authorization,//config('app.api_key'),
                'X-Client-Reference' => config('app.client_key')
            ]);
        }

        $base_url = config('app.geststudent_api')."api/";//"http://192.168.8.179/ProjetsLaravel/GestStudent/GestStudentApi/public/";

        switch ($method) {// a la sortie ici decrypter all la reponse e
            case 'GET':
                $req = Http::withHeaders($headers)->get($base_url . $url)->json();
                return $req && isset($req['success']) && $req['success'] ? encryptable($req, false) : $req;

            case 'POST':
                $req = Http::withHeaders($headers)->post($base_url . $url, $body)->json();
                return $req && isset($req['success']) && $req['success'] ? encryptable($req, false) : $req;

            case 'PUT':
                $req = Http::withHeaders($headers)->put($base_url . $url, $body)->json();
                return $req && isset($req['success']) && $req['success'] ? encryptable($req, false) : $req;

            case 'DELETE':
                $req = Http::withHeaders($headers)->delete($base_url . $url, $body)->json();
                return $req && isset($req['success']) && $req['success'] ? encryptable($req, false) : $req;

            default:
                return null;
        }

    }
}

