<?php

namespace App\Modules\Network\Helpers;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;

class CoreroSSP {
   
    protected $endpoint, $username, $password, $version, $retries;
 
    function __construct(string $_endpoint, int $_version, string $_username, string $_password, int $_retries = 1) {
        $this->endpoint = $_endpoint;
        $this->username = $_username;
        $this->password = $_password;
        $this->version = $_version;
        $this->retries = $_retries;
    }

    public function get_request($_path, $_parameters = []) {
        return Http::withBasicAuth($this->username, $this->password)
            ->retry($this->retries, 100)
            ->get("$this->endpoint/api/v$this->version/$_path" , $_parameters);
    }

    public function post_request($_path, $_parameters = []) {
        if(env('APP_ENV') != 'development') {
            return Http::withBasicAuth($this->username, $this->password)
                ->retry($this->retries, 100)
                ->post("$this->endpoint/api/v$this->version/$_path" , $_parameters);
        }

        return true;
    }

    public function delete_request($_path, $_parameters = []) {
        if(env('APP_ENV') != 'development') {
            return Http::withBasicAuth($this->username, $this->password)
                ->retry($this->retries, 100)
                ->delete("$this->endpoint/api/v$this->version/$_path" , $_parameters);
        }

        return true;
    }

    public function get_prefixes(string $_tenant_id) {
        return $this->get_request("tenant/$_tenant_id/assigned-asset")->collect();
    }

    public function post_prefix(string $_tenant_id, string $_ip, string $_name) {
        return $this->post_request("tenant/$_tenant_id/assigned-asset", [
            'ip' => $_ip,
            'name' => $_name
        ]);
    }

    public function delete_prefix(string $_tenant_id, string $_id) {
        return $this->delete_request("tenant/$_tenant_id/assigned-asset/$_id");
    }
}

?>