<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class SalesApi extends BaseApi
{

     /**
     * Operation Get Order Metrics
     *
     *  @param string|array $params['marketplaceIds'] A list of marketplace identifiers.
     *  @param string $params['interval'] A time interval used for selecting order metrics. This takes the form of two dates separated by two hyphens (first date is inclusive; second date is exclusive).
     *  @param string $params['granularityTimeZone'] An IANA-compatible time zone for determining the day boundary.
     *  @param enum $params['granularity'] The granularity of the grouping of order metrics, based on a unit of time.
     *  @param enum $params['buyerType'] Filters the results by the buyer type that you specify, B2B (business to business) or B2C (business to customer). Example: B2B, if you want the response to include order metrics for only B2B buyers.
     *  @param string|array $params['fulfillmentNetwork'] Filters the results by the fulfillment network that you specify, MFN (merchant fulfillment network) or AFN (Amazon fulfillment network). Do not include this filter if you want the response to include order metrics for all fulfillment networks. Example: AFN, if you want the response to include order metrics for only Amazon fulfillment network.
     *  @param enum $params['firstDayOfWeek'] Specifies the day that the week starts on when granularity=Week, either Monday or Sunday. Default: Monday. Example: Sunday, if you want the week to start on a Sunday.
     *  @param string $params['asin'] Filters the results by the ASIN that you specify. Specifying both ASIN and SKU returns an error. Do not include this filter if you want the response to include order metrics for all ASINs. Example: B0792R1RSN, if you want the response to include order metrics for only ASIN B0792R1RSN.
     *  @param string $params['sku '] Filters the results by the SKU that you specify. Specifying both ASIN and SKU returns an error. Do not include this filter if you want the response to include order metrics for all SKUs. Example: TestSKU, if you want the response to include order metrics for only SKU TestSKU.
     *
     * @return json Returns aggregated order metrics for given interval, broken down by granularity, for given buyer type.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/sales-api/sales.md#getordermetrics
     */
    public function getOrderMetrics($params)
    {
        if (!array_key_exists('marketplaceIds', $params) ||  null === $params['marketplaceIds']) {
            throw new \InvalidArgumentException('Missing the required parameter $marketplaceIds when calling getOrderItemsBuyerInfo');
        }

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

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

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

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

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

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

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

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

        if (array_key_exists('fulfillmentNetwork', $params) && $params['fulfillmentNetwork'] !== null) {
            $query_params['fulfillmentNetwork'] = $params['fulfillmentNetwork'];
        }
        if (array_key_exists('firstDayOfWeek', $params) && $params['firstDayOfWeek'] !== null) {
            $query_params['firstDayOfWeek'] = $params['firstDayOfWeek'];
        }
        if (array_key_exists('asin', $params) && $params['asin'] !== null) {
            $query_params['asin'] = $params['asin'];
        }
        if (array_key_exists('sku', $params) && $params['sku'] !== null) {
            $query_params['sku'] = $params['sku'];
        }

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

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