<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class ProductFeesApi extends BaseApi
{

    /**
     * Operation Get my Fees Estimate for sku
     *
     * @param array $body Request schema.
     *
     * @param string $seller_sku Used to identify an item in the given marketplace. SellerSKU is qualified by the seller's SellerId, which is included with every operation that you submit.
     *
     * @return
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-fees-api/productFeesV0.md#getmyfeesestimateforsku
     */
    public function getMyFeesEstimateForSKU($body, $seller_sku)
    {
        if (null === $body) {
            throw new \InvalidArgumentException('Missing the required parameter $body when calling getOrderItemsBuyerInfo');
        }

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

        $resource_path = '/products/fees/v0/listings/{SellerSKU}/feesEstimate';
        $resource_path1 = '/products/fees/v0/listings/{SellerSKU}/feesEstimate';
        $query_params = [];
        $http_body = $body;
        $method = 'POST';

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

            $resource_path1 = str_replace(
                '{' . 'SellerSKU' . '}',
                $seller_sku,
                $resource_path1
            );
        }

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

        $url = $this->config->getHost() . $resource_path1;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get my Fees Estimate for asin
     *
     * @param array $body Request schema.
     *
     * @param string $asin Used to identify an item in the given marketplace. SellerSKU is qualified by the seller's SellerId, which is included with every operation that you submit.
     *
     * @return
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref:- https://github.com/amzn/selling-partner-api-docs/blob/main/references/product-fees-api/productFeesV0.md#getmyfeesestimateforasin
     */

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

        if (null === $asin) {
            throw new \InvalidArgumentException('Missing the required parameter $asin when calling getOrderItemsBuyerInfo');
        }
        $resource_path = '/products/fees/v0/items/{Asin}/feesEstimate';
        $query_params = [];
        $http_body = $body;
        $method = 'POST';

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

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

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get MyFees Estimates Batchs
     *
     * @param string $params['MarketplaceId'] A marketplace identifier. Specifies the marketplace for which prices are returned. (required)
     * @param string $params['IdType'] (ASIN, SellerSKU)
     * @param string $params['IdValue'] If IdType is ASIN then value should be ASIN otherwise SKU
     * @param string $params['Amount'] Product price value
     * @param string $params['OptionalFulfillmentProgram'] (FBA_CORE, FBA_SNL. Default: FBA_CORE)
     * @param string $params['IsAmazonFulfilled'] (true, false. Default: true)
     *
     *
     * @return json Returns the estimated fees for a list of products.
     *
     * @author Jignesh Kapadia <jignesh@topsinfosolutions.com>
     *
     * About more Ref :- https://developer-docs.amazon.com/sp-api/docs/product-fees-api-v0-reference#getmyfeesestimates
     */
    public function getMyFeesEstimates($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 getMyFeesEstimates');
            }

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

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

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

        $resource_path = '/products/fees/v0/feesEstimate';

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

        $http_body=[];
        foreach ($params as $value)
        {
            $http_body[] = [
                'FeesEstimateRequest' => [
                    'MarketplaceId' => $value['MarketplaceId'],
                    'PriceToEstimateFees' => [
                        'ListingPrice' => [
                            'CurrencyCode' => (array_key_exists('CurrencyCode', $value) ? $value['CurrencyCode'] : 'USD'),
                            'Amount' => $value['Amount']
                        ]
                    ],
                    'Identifier' => time(),
                    "IsAmazonFulfilled"=> (array_key_exists('IsAmazonFulfilled', $value) ? (boolean) $value['IsAmazonFulfilled'] : true),
                    "OptionalFulfillmentProgram" => (array_key_exists('OptionalFulfillmentProgram', $value) ? $value['OptionalFulfillmentProgram'] : 'FBA_CORE'),
                ],
                'IdType' => $value['IdType'],
                'IdValue' => $value['IdValue'],
            ];
        }

        $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);
    }
}
