<?php

namespace WKO\Services\Koin\Requests;

use WKO\Controllers\Logs;
use WKO\Helpers\Config as HelpersConfig;
use WKO\Services\Koin\Config;

/**
 * Name: Request
 * Abstract class for requests
 * @package Koin\Requests
 * @since 1.0.0
 */
abstract class Request
{
    protected $body;
    protected $header;
    protected $method;
    protected $endpoint;

    /**
     * Send requests
     * @param array $header
     * @param array $body
     * @param string $url
     */
    protected function send()
    {
        $default_header = [
            'Accept'           => 'application/json',
            'Content-Type'     => 'application/json',
            'W-Module-Version' => HelpersConfig::__version(),
            'Authorization'    => $this->get_auth(),
            'Content-Length'   => $this->body ? strlen(json_encode($this->body)) : 0
        ];

        $header = is_array($this->header) ? array_merge($default_header, $this->header) : $default_header;

        $args = [
            'headers' => $header,
            'timeout' => 10000,
            'method'  => $this->method
        ];

        if ($this->body) {
            $args['body'] = json_encode($this->body);
        }

        if (get_option('wc_koin_settings_environment') === 'sandbox') {
            $args['user-agent'] = 'koin-oficial';
        }

        $log = new Logs;
        $response = wp_remote_request($this->endpoint, $args);

        if (!str_contains($this->endpoint, 'tokenize')) {
            $log->set_request_logs("KOIN REQUEST SENT", print_r([$this->endpoint, $args], true));
        }

        return $response;
    }

    protected function get_auth()
    {
        $token = get_option('wc_koin_settings_secret_key');

        return "Bearer $token";
    }

    /**
     * Get the full request url
     * @param string $base
     * @param array $parameters
     * @return string
     */
    protected function get_endpoint($base, $parameters = [])
    {
        $url = Config::request_domain() . $base;
        foreach ($parameters as $param) {
            if ($param) {
                $url .= "/{$param}";
            }
        }

        return $url;
    }
}
