<?php

namespace App\Tools;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class SendinBlueEmailService
{

    public function getApiCall($endpoint){

        $currentUrl = env('SENDINBLUE_MAIL_BASE_URL') . $endpoint;
        try {
            $headers = [
                'api-key' => env('SENDINBLUE_MAIL_API_KEY'),
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
            ];

            $client = new Client([
                'base_uri' => env('SENDINBLUE_MAIL_BASE_URL')
            ]);

            $response = $client->get($endpoint,[
                'headers' => $headers,
            ]);

            $finalResponse = json_decode($response->getBody()->getContents(), true);

            $this->appLog("Sending Request To SendinBlue Api was successful | Request Type: GET | Status: Successful | Endpoint: {$currentUrl} | Response " . json_encode($finalResponse));
        }
        catch (\Exception $exception){
            $this->appLog("Sending Request To SendinBlue Api Failed | Request Type: GET | Status: Failed | Endpoint: {$currentUrl} | Response " . json_encode($exception->getMessage()));
            $finalResponse = null;
        }

        return $finalResponse;

    }

    public function postApiCall($endpoint, $formData = []){
        $currentUrl = env('SENDINBLUE_MAIL_BASE_URL') . $endpoint;
        try {
            $headers = [
                'api-key' => env('SENDINBLUE_MAIL_API_KEY'),
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
            ];

            $client = new Client([
                'base_uri' => env('SENDINBLUE_MAIL_BASE_URL')
            ]);

            $response = $client->post($endpoint, [
                'headers' => $headers,
                'json' => $formData,
            ]);

//            dd($response->getBody()->getContents());
            $finalResponse = json_decode($response->getBody()->getContents(), true);

            $this->appLog("Sending Request To SendinBlue Api was successful | Request Type: POST | Status: Successful | Endpoint: {$currentUrl} | formData: ". json_encode($formData)." | Response " . json_encode($finalResponse));

        }
        catch (\Exception $exception){
            $this->appLog("Sending Request To SendinBlue Api Failed | Request Type: POST | Status: Failed | Endpoint: {$currentUrl} | Response " . json_encode($exception->getMessage()));
            $finalResponse = null;
        }

        return $finalResponse;
    }

    public function deleteApiCall($endpoint){

        $currentUrl = env('SENDINBLUE_MAIL_BASE_URL') . $endpoint;
        try {
            $headers = [
                'api-key' => env('SENDINBLUE_MAIL_API_KEY'),
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
            ];

            $client = new Client([
                'base_uri' => env('SENDINBLUE_MAIL_BASE_URL')
            ]);

            $response = $client->delete($endpoint,[
                'headers' => $headers,
            ]);

            $finalResponse = json_decode($response->getBody()->getContents(), true);

            $this->appLog("Sending Request To SendinBlue Api was successful | Request Type: DELETE | Status: Successful | Endpoint: {$currentUrl} | Response " . json_encode($finalResponse));
        }
        catch (\Exception $exception){
            $this->appLog("Sending Request To SendinBlue Api Failed | Request Type: DELETE | Status: Failed | Endpoint: {$currentUrl} | Response " . json_encode($exception->getMessage()));
            $finalResponse = null;
        }

        return $finalResponse;

    }


    public function appLog($message)
    {
        Log::channel('api_request')->info("\n");
        Log::channel('api_request')->info($message);
    }

}
