<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class ProductPricingApi extends BaseApi
{
    /**
     * Operation Get Pricing
     *
     * @param array $params
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param string|array $params['Asins'] A list of up to twenty Amazon Standard Identification Number (ASIN) values used to identify items in the given marketplace. <Max Count 20>
     * @param string|array $params['Skus'] A list of up to twenty seller SKU values used to identify items in the given marketplace. <Max count 20>
     * @param enum $params['ItemType'] Indicates whether ASIN values or seller SKU values are used to identify items. If you specify Asin, the information in the response will be dependent on the list of Asins you provide in the Asins parameter. If you specify Sku, the information in the response will be dependent on the list of Skus you provide in the Skus parameter. (required)
     * @param enum $params['ItemCondition'] Filters the offer listings based on item condition. Possible values: New, Used, Collectible, Refurbished, Club.
     *
     * @return json Returns pricing information for a seller's offer listings based on seller SKU or ASIN.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref:- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-pricing-api/productPricingV0.md#getpricing
     */
    public function getPricing($params)
    {
        if (!array_key_exists('ItemType', $params) ||  null === $params['ItemType']) {
            throw new \InvalidArgumentException('Missing the required parameter $ItemType when calling getOrderItemsBuyerInfo');
        }

        if (!array_key_exists('MarketplaceId', $params) ||  null === $params['MarketplaceId']) {
            throw new \InvalidArgumentException('Missing the required parameter $MarketplaceId when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/products/pricing/v0/price';

        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // Get Query Params
        if (array_key_exists('MarketplaceId', $params) && $params['MarketplaceId'] !== null) {
            $query_params['MarketplaceId'] = $params['MarketplaceId'];
        }
        if (array_key_exists('Asins', $params) && $params['Asins'] !== null) {
            $query_params['Asins'] = is_array($params['Asins']) ? implode(',', $params['Asins']) :  $params['Asins'];
        }

        if (array_key_exists('Skus', $params) && $params['Skus'] !== null) {
            $query_params['Skus'] = is_array($params['Skus']) ? implode(',', $params['Skus']) :  $params['Skus'];
        }

        if (array_key_exists('ItemType', $params) && $params['ItemType'] !== null) {
            $query_params['ItemType'] = $params['ItemType'];
        }

        if (array_key_exists('ItemCondition', $params) && $params['ItemCondition'] !== null) {
            $query_params['ItemCondition'] = $params['ItemCondition'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Cometitive Pricing
     *
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param string|array $params['Asins'] A list of up to twenty Amazon Standard Identification Number (ASIN) values used to identify items in the given marketplace.
     * @param string|array $params['Skus'] A list of up to twenty seller SKU values used to identify items in the given marketplace.
     * @param enum $params['ItemType'] Indicates whether ASIN values or seller SKU values are used to identify items.
     *
     * @return json Returns competitive pricing information for a seller's offer listings based on seller SKU or ASIN.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref:- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-pricing-api/productPricingV0.md#getcompetitivepricing
     */
    public function getCompetitivePricing($params)
    {
        if (!array_key_exists('MarketplaceId', $params) ||  null === $params['MarketplaceId']) {
            throw new \InvalidArgumentException('Missing the required parameter $MarketplaceId when calling getOrderItemsBuyerInfo');
        }

        if (!array_key_exists('ItemType', $params) ||  null === $params['ItemType']) {
            throw new \InvalidArgumentException('Missing the required parameter $ItemType when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/products/pricing/v0/competitivePrice';

        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // Get Query Params
        if (array_key_exists('MarketplaceId', $params) && $params['MarketplaceId'] !== null) {
            $query_params['MarketplaceId'] = $params['MarketplaceId'];
        }
        if (array_key_exists('Asins', $params) && $params['Asins'] !== null) {
            $query_params['Asins'] = is_array($params['Asins']) ? implode(',', $params['Asins']) :  $params['Asins'];
        }

        if (array_key_exists('Skus', $params) && $params['Skus'] !== null) {
            $query_params['Skus'] = is_array($params['Skus']) ? implode(',', $params['Skus']) :  $params['Skus'];
        }

        if (array_key_exists('ItemType', $params) && $params['ItemType'] !== null) {
            $query_params['ItemType'] = $params['ItemType'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Listing offers
     *
     * @param array $params
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param $seller_sku Identifies an item in the given marketplace. SellerSKU is qualified by the seller's SellerId, which is included with every operation that you submit. (required)
     * @param string $params['ItemCondition'] Filters the offer listings based on item condition. Possible values: New, Used, Collectible, Refurbished, Club. (required)
     *
     * @return json Returns the lowest priced offers for a single SKU listing.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-pricing-api/productPricingV0.md#getlistingoffers
     */
    public function getListingOffers($params, $seller_sku)
    {
        if (!array_key_exists('MarketplaceId', $params) ||  null === $params['MarketplaceId']) {
            throw new \InvalidArgumentException('Missing the required parameter $MarketplaceId when calling getOrderItemsBuyerInfo');
        }

        if (null === $seller_sku) {
            throw new \InvalidArgumentException('Missing the required parameter $seller_sku when calling getOrderItemsBuyerInfo');
        }

        if (!array_key_exists('ItemCondition', $params) ||  null === $params['ItemCondition']) {
            throw new \InvalidArgumentException('Missing the required parameter $ItemCondition when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/products/pricing/v0/listings/{SellerSKU}/offers';

        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // get Query params
        if (array_key_exists('MarketplaceId', $params) && $params['MarketplaceId'] !== null) {
            $query_params['MarketplaceId'] = $params['MarketplaceId'];
        }

        if (array_key_exists('ItemCondition', $params) && $params['ItemCondition'] !== null) {
            $query_params['ItemCondition'] =  $params['ItemCondition'];
        }

        // path params
        if (null !== $seller_sku) {
            $resource_path = str_replace(
                '{' . 'SellerSKU' . '}',
                $seller_sku,
                $resource_path
            );
        }

        $url = $this->config->getHost() . $resource_path;

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Item Offers
     *
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param $asin The Amazon Standard Identification Number (ASIN) of the item. (required)
     * @param string $params['ItemCondition'] Filters the offer listings to be considered based on item condition. Possible values: New, Used, Collectible, Refurbished, Club. (required)
     *
     *
     * @return json Returns the lowest priced offers for a single item based on ASIN.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-pricing-api/productPricingV0.md#getitemoffers
     */
    public function getItemOffers($params, $asin)
    {
        if (!array_key_exists('MarketplaceId', $params) ||  null === $params['MarketplaceId']) {
            throw new \InvalidArgumentException('Missing the required parameter $MarketplaceId when calling getOrderItemsBuyerInfo');
        }

        if (null === $asin) {
            throw new \InvalidArgumentException('Missing the required parameter $asin when calling getOrderItemsBuyerInfo');
        }

        if (!array_key_exists('ItemCondition', $params) ||  null === $params['ItemCondition']) {
            throw new \InvalidArgumentException('Missing the required parameter $ItemCondition when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/products/pricing/v0/items/{Asin}/offers';

        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // get Querry Params
        if (array_key_exists('MarketplaceId', $params) && $params['MarketplaceId'] !== null) {
            $query_params['MarketplaceId'] = $params['MarketplaceId'];
        }

        if (array_key_exists('ItemCondition', $params) && $params['ItemCondition'] !== null) {
            $query_params['ItemCondition'] =  $params['ItemCondition'];
        }

        // path params
        if (null !== $asin) {
            $resource_path = str_replace(
                '{' . 'Asin' . '}',
                $asin,
                $resource_path
            );
        }

        $url = $this->config->getHost() . $resource_path;

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Item Offers Batchs
     *
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param string $params['asin'] The Amazon Standard Identification Number (ASIN) of the item. (required)
     * @param string $params['ItemCondition'] Filters the offer listings to be considered based on item condition. Possible values: New, Used, Collectible, Refurbished, Club. (required)
     *
     *
     * @return json Returns the lowest priced offers for a batch of items based on ASIN.
     *
     * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
     *
     * About more Ref :- https://developer-docs.amazon.com/sp-api/docs/product-pricing-api-v0-reference#getitemoffersbatch
     */
    public function getItemOffersBatch($params)
    {
        if (count($params) > 20) {
            throw new \InvalidArgumentException('Request parameters should not more than 20.');
        }

        if (empty($params)) {
            throw new \InvalidArgumentException('Request parameters should not empty');
        }

        foreach ($params as $value)
        {
            if (!array_key_exists('MarketplaceId', $value) ||  empty($value['MarketplaceId'])) {
                throw new \InvalidArgumentException('Missing the required parameter MarketplaceId when calling getItemOffersBatch');
            }

            if (!array_key_exists('asin', $value) ||  empty($value['asin'])) {
                throw new \InvalidArgumentException('Missing the required parameter asin when calling getItemOffersBatch');
            }

            if (!array_key_exists('ItemCondition', $value) ||  empty($value['ItemCondition'])) {
                throw new \InvalidArgumentException('Missing the required parameter ItemCondition when calling getItemOffersBatch');
            }
        }

        $resource_path = '/batches/products/pricing/v0/itemOffers';

        $query_params = [];
        $method = 'POST';

        $http_body=[];
        foreach ($params as $value)
        {
            $http_body['requests'][] = [
                'uri' => '/products/pricing/v0/items/'.$value['asin'].'/offers',
                'method' => 'GET',
                'MarketplaceId' => $value['MarketplaceId'],
                'ItemCondition' => $value['ItemCondition']
            ];
        }

        $url = $this->config->getHost() . $resource_path;

        $curl_headers = $this->generateRequest($resource_path, $query_params, json_encode($http_body), $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Listing Offers Batchs
     *
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param string $params['sku'] Identifies an item in the given marketplace. SellerSKU is qualified by the seller's SellerId, which is included with every operation that you submit. (required)
     * @param string $params['ItemCondition'] Filters the offer listings to be considered based on item condition. Possible values: New, Used, Collectible, Refurbished, Club. (required)
     *
     *
     * @return json Returns the lowest priced offers for a batch of listings by SKU.
     *
     * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
     *
     * About more Ref :- https://developer-docs.amazon.com/sp-api/docs/product-pricing-api-v0-reference#getlistingoffersbatch
     */
    public function getListingOffersBatch($params)
    {
        if (count($params) > 20) {
            throw new \InvalidArgumentException('Request parameters should not more than 20.');
        }

        if (empty($params)) {
            throw new \InvalidArgumentException('Request parameters should not empty');
        }

        foreach ($params as $value)
        {
            if (!array_key_exists('MarketplaceId', $value) ||  empty($value['MarketplaceId'])) {
                throw new \InvalidArgumentException('Missing the required parameter MarketplaceId when calling getListingOffersBatch');
            }

            if (!array_key_exists('sku', $value) ||  empty($value['sku'])) {
                throw new \InvalidArgumentException('Missing the required parameter sku when calling getListingOffersBatch');
            }

            if (!array_key_exists('ItemCondition', $value) ||  empty($value['ItemCondition'])) {
                throw new \InvalidArgumentException('Missing the required parameter ItemCondition when calling getListingOffersBatch');
            }
        }

        $resource_path = '/batches/products/pricing/v0/listingOffers';

        $query_params = [];
        $method = 'POST';

        $http_body=[];
        foreach ($params as $value)
        {
            $http_body['requests'][] = [
                'uri' => '/products/pricing/v0/listings/'.$value['sku'].'/offers',
                'method' => 'GET',
                'MarketplaceId' => $value['MarketplaceId'],
                'ItemCondition' => $value['ItemCondition']
            ];
        }

        $url = $this->config->getHost() . $resource_path;

        $curl_headers = $this->generateRequest($resource_path, $query_params, json_encode($http_body), $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
}
