<?php

namespace App\Services\General;

use App\Models\RequestLog;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Request;

class CustomHttpClientService
{

    protected string $baseUrl;
    protected array $headers = [];
    protected bool $saveLog;

    public function __construct($baseUrl = '', $saveLog = true)
    {
        $this->baseUrl = $baseUrl;
        $this->saveLog = $saveLog;
    }

    /**
     * @param array $headers
     * @return $this
     */
    public function withHeaders(array $headers): static
    {
        $this->headers = $headers;
        return $this;
    }

    /**
     * @param $endpoint
     * @param array $queryParams
     * @return array
     */
    public function get($endpoint, array $queryParams = []): array
    {
        $url = $this->buildUrl($endpoint);
        $response = Http::withHeaders($this->headers)->get($url, $queryParams);
        $this->addLog([
            'status_code' => $response->status(),
            'method' => 'get',
            'url' => $url,
            'data' => $queryParams,
            'headers' => $this->headers,
            'result' => $response->json(),
        ]);
        return [
            'response' => $response->json(),
            'status_code' => $response->status(),
        ];
    }

    /**
     * @param $endpoint
     * @param $data
     * @return array
     */
    public function post($endpoint, $data): array
    {
        $url = $this->buildUrl($endpoint);
        $response = Http::withHeaders($this->headers)->post($url, $data);
        $this->addLog([
            'status_code' => $response->status(),
            'method' => 'post',
            'url' => $url,
            'data' => $data,
            'headers' => $this->headers,
            'result' => $response->json(),
        ]);
        return [
            'response' => $response->json(),
            'status_code' => $response->status(),
        ];
    }

    /**
     * @param $endpoint
     * @param array $data
     * @return array
     */
    public function put($endpoint, array $data = []): array
    {
        $url = $this->buildUrl($endpoint);
        $response = Http::withHeaders($this->headers)->put($url, $data);
        $this->addLog([
            'status_code' => $response->status(),
            'method' => 'put',
            'url' => $url,
            'data' => $data,
            'headers' => $this->headers,
            'result' => $response->json(),
        ]);
        return [
            'response' => $response->json(),
            'status_code' => $response->status(),
        ];
    }

    /**
     * @param $endpoint
     * @param array $data
     * @return array
     */
    public function delete($endpoint, array $data = []): array
    {
        $url = $this->buildUrl($endpoint);
        $response = Http::withHeaders($this->headers)->delete($url, $data);
        $this->addLog([
            'status_code' => $response->status(),
            'method' => 'delete',
            'url' => $url,
            'data' => $data,
            'headers' => $this->headers,
            'result' => $response->json(),
        ]);
        return [
            'response' => $response->json(),
            'status_code' => $response->status(),
        ];
    }

    /**
     * @param $endpoint
     * @return string
     */
    protected function buildUrl($endpoint): string
    {
        if (!$this->baseUrl){return $endpoint;}
        return rtrim($this->baseUrl, '/') . '/' . ltrim($endpoint, '/');
    }

    /**
     * @param array $data
     * @return void
     */
    private function addLog(array $data = []): void
    {
        if (!$this->saveLog) {
            return;
        }
        $data['ip'] = Request::ip();
        RequestLog::create($data);
    }

} //end of class
