<?php 

namespace Foxstudio\Plugins\Template\Repository;

class ProductRepository {
    public string $serverUrl;

    public function __construct($serverUrl) {
        $this->serverUrl = $serverUrl;
    }
    
    public function getProductInfo($sku) {
        $args = [
            'headers' => [
                'content-type' => 'application/json',
                'api_key' => API_KEY
            ],
            'body' => [
                "sku" => $sku
            ]
        ];
        $results = wp_remote_get($this->serverUrl . "/api/get_product_details", $args);
        $results = json_decode($results['body'], true);
        return $results;
    }

    public function getDeliveryInfo($sku) {
        $args = [
            'headers' => [
                'content-type' => 'application/json',
                'api_key' => API_KEY
            ],
            'body' => [
                "sku" => $sku
            ]
        ];
        $results = wp_remote_get($this->serverUrl . "/api/get_product_details", $args);

        $results = json_decode($results['body'], true);
        return $results['message']['inventory'];
    }

    public function getLabelingInfo($sku) {
        $args = [
            'headers' => [
                'content-type' => 'application/json',
                'api_key' => API_KEY
            ],
            'body' => [
                "sku" => $sku
            ]
        ];
        $results = wp_remote_get($this->serverUrl . "/api/get_product_details", $args);
        $results = json_decode($results['body'], true);
        return $results['message']['labeling'];
    }

    public function getLowestPrice($price) {
        $args = [
            'headers' => [
                'content-type' => 'application/json',
                'api_key' => API_KEY
            ],
            'body' => [
                "price" => $price
            ]
        ];
        $results = wp_remote_get($this->serverUrl . "/api/getLowestPrice", $args);
        $results = json_decode($results['body'], true);
        return $results['message']['lowestPrice'];
    }

    public function getProductDescription($sku) {
        $args = [
            'headers' => [
                'content-type' => 'application/json',
                'api_key' => API_KEY
            ],
            'body' => [
                "sku" => $sku
            ]
        ];
        $results = wp_remote_get($this->serverUrl . "/api/get_product_description", $args);
        $results = json_decode($results['body'], true);
        return $results['message'];
    }

}
